You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<divclass="vueschool"><ahref="https://vueschool.io/lessons/vuejs-computed-properties?friend=vuejs"target="_blank"rel="sponsored noopener"title="Learn how computed properties work with Vue School">Learn how computed properties work with a free lesson on Vue School</a></div>
5
+
[Learn how computed properties work with a free lesson on Vue School](https://vueschool.io/lessons/vuejs-computed-properties?friend=vuejs)
6
6
7
7
In-template expressions are very convenient, but they are meant for simple operations. Putting too much logic in your templates can make them bloated and hard to maintain. For example:
8
8
@@ -29,18 +29,21 @@ That's why for any complex logic, you should use a **computed property**.
29
29
constvm=Vue.createApp().mount(
30
30
{
31
31
data: {
32
-
message:"Hello"
32
+
message:'Hello'
33
33
},
34
34
computed: {
35
35
// a computed getter
36
36
reversedMessage() {
37
37
// `this` points to the vm instance
38
-
returnthis.message.split("").reverse().join("")
38
+
returnthis.message
39
+
.split('')
40
+
.reverse()
41
+
.join('')
39
42
}
40
43
}
41
44
},
42
-
"#example"
43
-
);
45
+
'#example'
46
+
)
44
47
```
45
48
46
49
Result:
@@ -50,9 +53,9 @@ Result:
50
53
Here we have declared a computed property `reversedMessage`. The function we provided will be used as the getter function for the property `vm.reversedMessage`:
51
54
52
55
```js
53
-
console.log(vm.reversedMessage);// => 'olleH'
54
-
vm.message="Goodbye";
55
-
console.log(vm.reversedMessage);// => 'eybdooG'
56
+
console.log(vm.reversedMessage) // => 'olleH'
57
+
vm.message='Goodbye'
58
+
console.log(vm.reversedMessage) // => 'eybdooG'
56
59
```
57
60
58
61
You can open the sandbox(TODO) and play with the example vm yourself. The value of `vm.reversedMessage` is always dependent on the value of `vm.message`.
@@ -101,37 +104,43 @@ Vue does provide a more generic way to observe and react to data changes on a Vu
101
104
```
102
105
103
106
```js
104
-
constvm=Vue.createApp().mount({
105
-
data: {
106
-
firstName:"Foo",
107
-
lastName:"Bar",
108
-
fullName:"Foo Bar"
109
-
},
110
-
watch: {
111
-
firstName(val) {
112
-
this.fullName= val +""+this.lastName;
107
+
constvm=Vue.createApp().mount(
108
+
{
109
+
data: {
110
+
firstName:'Foo',
111
+
lastName:'Bar',
112
+
fullName:'Foo Bar'
113
113
},
114
-
lastName(val) {
115
-
this.fullName=this.firstName+""+ val;
114
+
watch: {
115
+
firstName(val) {
116
+
this.fullName= val +''+this.lastName
117
+
},
118
+
lastName(val) {
119
+
this.fullName=this.firstName+''+ val
120
+
}
116
121
}
117
-
}
118
-
}, '#demo');
122
+
},
123
+
'#demo'
124
+
)
119
125
```
120
126
121
127
The above code is imperative and repetitive. Compare it with a computed property version:
this.answer='Questions usually contain a question mark. ;-)'
266
-
return
267
-
}
268
-
this.answer='Thinking...'
269
-
var vm =this
270
-
axios.get('https://yesno.wtf/api')
271
-
.then(function (response) {
272
-
vm.answer=_.capitalize(response.data.answer)
273
-
})
274
-
.catch(function (error) {
275
-
vm.answer='Error! Could not reach the API. '+ error
276
-
})
277
-
}
278
-
}
279
-
})
280
-
</script>
281
-
{% endraw %}
245
+
<computed-2 />
282
246
283
247
In this case, using the `watch` option allows us to perform an asynchronous operation (accessing an API), limit how often we perform that operation, and set intermediary states until we get a final answer. None of that would be possible with a computed property.
284
248
285
-
In addition to the `watch` option, you can also use the imperative [vm.\$watch API](../api/#vm-watch).
249
+
In addition to the `watch` option, you can also use the imperative [vm.\$watch API](TODO:../api/#vm-watch).
0 commit comments