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
* chore: update computed properties example
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
* Update src/guide/computed.md
Co-authored-by: Ben Hong <[email protected]>
Co-authored-by: Ben Hong <[email protected]>
Copy file name to clipboardExpand all lines: src/guide/computed.md
+46-28
Original file line number
Diff line number
Diff line change
@@ -2,45 +2,69 @@
2
2
3
3
## Computed Properties
4
4
5
-
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:
5
+
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, if we have an object with a nested array:
6
+
7
+
```js
8
+
Vue.createApp({
9
+
data() {
10
+
return {
11
+
author: {
12
+
name:'John Doe',
13
+
books: [
14
+
'Vue 2 - Advanced Guide',
15
+
'Vue 3 - Basic Guide',
16
+
'Vue 4 - The Mystery'
17
+
]
18
+
}
19
+
}
20
+
}
21
+
})
22
+
```
23
+
24
+
And we want to display different messages depending on if `author` already has some books or not
At this point, the template is no longer simple and declarative. You have to look at it for a second before realizing that it displays `message` in reverse. The problem is made worse when you want to include the reversed message in your template more than once.
33
+
At this point, the template is no longer simple and declarative. You have to look at it for a second before realizing that it performs a calculation depending on `author.books`. The problem is made worse when you want to include this calculation in your template more than once.
14
34
15
35
That's why for complex logic that includes reactive data, you should use a **computed property**.
Here we have declared a computed property `reversedMessage`. The function we provided will be used as the getter function for the property `vm.reversedMessage`:
56
-
57
-
```js
58
-
console.log(vm.reversedMessage) // => 'olleH'
59
-
vm.message='Goodbye'
60
-
console.log(vm.reversedMessage) // => 'eybdooG'
61
-
```
79
+
Here we have declared a computed property `publishedBooksMessage`.
62
80
63
-
Try to change the value of `message`in the application `data` and you will see how `reversedMessage` is changing accordingly.
81
+
Try to change the value of `books` array in the application `data` and you will see how `publishedBooksMessage` is changing accordingly.
64
82
65
-
You can data-bind to computed properties in templates just like a normal property. Vue is aware that `vm.reversedMessage` depends on `vm.message`, so it will update any bindings that depend on `vm.reversedMessage` when `vm.message` changes. And the best part is that we've created this dependency relationship declaratively: the computed getter function has no side effects, which makes it easier to test and understand.
83
+
You can data-bind to computed properties in templates just like a normal property. Vue is aware that `vm.publishedBooksMessage` depends on `vm.author.books`, so it will update any bindings that depend on `vm.publishedBooksMessage` when `vm.author.books` changes. And the best part is that we've created this dependency relationship declaratively: the computed getter function has no side effects, which makes it easier to test and understand.
66
84
67
85
### Computed Caching vs Methods
68
86
69
87
You may have noticed we can achieve the same result by invoking a method in the expression:
70
88
71
89
```html
72
-
<p>Reversed message: "{{ reverseMessage() }}"</p>
90
+
<p>{{ calculateBooksMessage() }}</p>
73
91
```
74
92
75
93
```js
76
94
// in component
77
95
methods: {
78
-
reverseMessage() {
79
-
returnthis.message.split('').reverse().join('')
96
+
calculateBooksMessage()() {
97
+
returnthis.author.books.length>0?'Yes':'No'
80
98
}
81
99
}
82
100
```
83
101
84
-
Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that **computed properties are cached based on their reactive dependencies.** A computed property will only re-evaluate when some of its reactive dependencies have changed. This means as long as `message` has not changed, multiple access to the `reversedMessage` computed property will immediately return the previously computed result without having to run the function again.
102
+
Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that **computed properties are cached based on their reactive dependencies.** A computed property will only re-evaluate when some of its reactive dependencies have changed. This means as long as `author.books` has not changed, multiple access to the `publishedBooksMessage` computed property will immediately return the previously computed result without having to run the function again.
85
103
86
104
This also means the following computed property will never update, because `Date.now()` is not a reactive dependency:
0 commit comments