Learn how to use Vue to calculate attribute cache calculations
What are calculated properties
In Vue.js, you can use parentheses to output any data value:
<template>
<p>{{ count }}</p>
</template>
<script>
export default {
data() {
return {
count: 1
}
}
}
</script>
This property can host some small calculations, such as
<template>
{{ count * 10 }}
</template>
But you can only use one JavaScriptexpression.
In addition to this technical limitation, you also need to consider that the template should only be related to displaying data to the user, and should not perform logical calculations.
To do more with a single expression and have more declarative templates, you can useCalculated attributes.
The calculated properties are incomputed
Properties of Vue components:
<script>
export default {
computed: {
}
}
</script>
Examples of calculated attributes
This is a sample code using calculated attributescount
Calculate the output. note:
- I don't need to call
count()
. Vue.js automatically calls this function I used regular functions (instead of arrow functions) to define
count
Calculated properties, because I need to be able to access the component instance in the following waythis
.<template> <p>{{ count }}</p> </template>
<script> export default { data() { return { items: [1, 2, 3] } }, computed: { count: function() { return 'The count is ’ + this.items.length * 10 } } } </script>
Calculation properties and methods
If you already knowVue method, You may wonder what the difference is.
First, the method must be called, not just the reference method, so you need to callcount()
insteadcount
If you have onecount
method:
<script>
export default {
data() {
return {
items: [1, 2, 3]
}
},
methods: {
count: function() {
return 'The count is ' + this.items.length * 10
}
}
}
</script>
But the main difference isCalculated attributes are cached.
resultcount
The calculated attributes are cached internally untilitems
Data attribute changes.
Important note: The calculated attributes areUpdate only when the reactive source is updated. The regular JavaScript method is not passive, so a common example is to useDate.now()
:
<template>
<p>{{ now }}</p>
</template>
<script>
export default {
computed: {
now: function () {
return Date.now()
}
}
}
</script>
It will render once, and then it will not update even if the component is re-rendered. When the Vue component exits and reinitializes, just refresh the page.
In this case, a method is more suitable for your needs.
Download mine for freeVue manual
More vue tutorials:
- Vue.js overview
- Vue.js CLI: Learn how to use it
- Vue.js development tools
- Configure VS code for Vue development
- Create your first application with Vue.js
- Vue.js single file component
- Vue.js templates and interpolation
- Vue.js instructions
- Vue.js method
- Vue.js calculated attributes
- Use CSS to style Vue.js components
- Vue.js observer
- Vue method vs observer vs calculated property
- Vue.js filter
- Vue.js components
- Vue.js slot
- Vue.js component props
- Vue.js activities
- Vue.js component communication
- Vuex, Vue.js state manager
- Vue, use a component inside another component
- Vue, how to use prop as a class name
- How to use SCSS with Vue.js single file component
- Use Tailwind in Vue.js
- Vue router
- Dynamically display Vue components
- Vue.js cheat sheet
- Use Vuex to store Vue data to localStorage
- How to use Vue dynamic application class
- Vue, how to use the v model
- Vue, why data must be functions
- Roadmap to become a Vue.js developer in 2020