Home >Web Front-end >Vue.js >How to calculate function return value in vue
Vue function return value can be calculated in three ways: Calculated property: Define a function that calculates a value based on component data. Its return value will be cached and will only be recalculated when the dependent data changes. Method: Define a function that recalculates the value each time it is called. Template inline calculation: Use JavaScript expressions to perform calculations directly in the template.
Calculation of function return value in Vue
Function in Vue can return any JavaScript expression result, including calculation value.
Computed Properties
The most common way is to use computed properties. A computed property is a function that returns a value calculated based on component data. The return value of a computed property is cached, which means it is only recalculated when the dependent data changes. For example:
<code class="javascript">computed: { total() { return this.price * this.quantity; } }</code>
Method
Function can also be used as a method to return a calculated value. Methods are not cached, so they are recalculated on each call. For example:
<code class="javascript">methods: { calculateTotal() { return this.price * this.quantity; } }</code>
Inline calculation in template
You can also use JavaScript expressions to perform calculations directly in the template. For example:
<code class="html"><template> <div>{{ price * quantity }}</div> </template></code>
Notes
You need to pay attention to the following points:
this.$set
or Vue.set
. {{}}
syntax. The above is the detailed content of How to calculate function return value in vue. For more information, please follow other related articles on the PHP Chinese website!