Computed Properties
Vue.js’ inline expressions are very convenient, but the best use cases for them are simple boolean operations or string concatenations. For more complicated logic, you should use computed properties.
In Vue.js, you define computed properties with the computed
option:
1 | var demo = new Vue({ |
When you only need the getter, you can provide a single function instead of an object:
1 | // ... |
A computed property is essentially a property defined with getter/setter functions. You can use a computed property just like a normal property, but when you access it, you get the value returned by the getter function; when you change its value, you trigger the setter function passing in the new value as its argument.
Before Vue.js 0.11 there used to be a dependency collection gotcha which requires users to explicitly list dependencies when conditional statements are involved. Starting with 0.11 it is no longer necessary to do so.
Next, let’s learn about how to write a custom directive.