Skip to content

How to Pass Parameters in Computed Properties in Vue.js

Introduction:

If you wonder whether it is possible to pass parameters to computed properties in Vue.js, you should read this tutorial. In Vue.js, computed properties are cached based on reactive dependencies and do not accept parameters. If you need to pass arguments to dynamically calculate a value, you should use a method instead. Here's how to handle parameterized logic in Vue.js:

Define a computed property:

First, define a computed property in your Vue application using the computed option. You can give it a name and specify a function that will be used to calculate the value based on reactive data. For example:

Define a computed property in Vue.js

javascript
const app = Vue.createApp({
  data() {
    return {
      radius: 5
    }
  },
  computed: {
    circleArea() {
      // calculate circle area based on radius
      return Math.PI * this.radius * this.radius;
    }
  }
})
app.mount('#app')

In this example, a computed property called circleArea is defined, which calculates the area of a circle based on the value of radius in the data object.

Use methods for parameterized logic:

Since computed properties cannot accept parameters, you can define a method to handle arguments. For example, you can create a method that accepts a multiplier parameter:

Use a method for parameterized logic in Vue.js

javascript
const app = Vue.createApp({
  data() {
    return {
      radius: 5
    }
  },
  methods: {
    circleAreaWithMultiplier(multiplier) {
      // calculate circle area based on radius and multiplier
      return Math.PI * this.radius * this.radius * multiplier;
    }
  }
})
app.mount('#app')

In this updated example, the circleAreaWithMultiplier method accepts a multiplier argument, which is used in the calculation of the circle area.

Use the computed property and method in templates:

To use both in your template, access the computed property directly and call the method with your desired arguments. For example:

How to Pass Parameters in Computed Properties in Vue.js

vue
<template>
  <div>
    <p>Circle area with radius 5: {{ circleArea }}</p>
    <p>Circle area with radius 5 and multiplier 2: {{ circleAreaWithMultiplier(2) }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      radius: 5
    }
  },
  computed: {
    circleArea() {
      // calculate circle area based on radius
      return Math.PI * this.radius * this.radius;
    }
  },
  methods: {
    circleAreaWithMultiplier(multiplier) {
      // calculate circle area based on radius and multiplier
      return Math.PI * this.radius * this.radius * multiplier;
    }
  }
}
</script>

In this example, the circleArea computed property is used without any parameters, while the circleAreaWithMultiplier method is called with a multiplier argument passed directly in the template.

Conclusion:

By following these steps, you can correctly handle parameterized logic in Vue.js. Remember that computed properties are cached and only re-evaluate when their reactive dependencies change, making them ideal for derived state. For logic that requires arguments or dynamic updates based on passed values, use methods instead.

Dual-run preview — compare with live Symfony routes.