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
Copy file name to clipboardExpand all lines: src/guide/computed.md
+35-2Lines changed: 35 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,39 @@ You can open the console and play with the example vm yourself. The value of `vm
77
77
78
78
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 is pure and has no side effects, which makes it easy to test and reason about.
79
79
80
+
### Computed Caching vs Methods
81
+
82
+
You may have noticed we can achieve the same result by invoking a method in the expression:
83
+
84
+
```html
85
+
<p>Reversed message: "{{ reverseMessage() }}"</p>
86
+
```
87
+
88
+
```js
89
+
// in component
90
+
methods: {
91
+
reverseMessage:function () {
92
+
returnthis.message.split('').reverse().join('')
93
+
}
94
+
}
95
+
```
96
+
97
+
Instead of a computed property, we can define the same function as a method instead. For the end result, the two approaches are indeed exactly the same. However, the difference is that **computed properties are cached based on its dependencies.** A computed property will only re-evaluate when some of its 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.
98
+
99
+
This also means the following computed property will never update, because `Date.now()` is not a reactive dependency:
100
+
101
+
```js
102
+
computed: {
103
+
now:function () {
104
+
returnDate.now()
105
+
}
106
+
}
107
+
```
108
+
109
+
In comparison, a method invocation will **always** run the function whenever a re-render happens.
110
+
111
+
Why do we need caching? Imagine we have an expensive computed property **A**, which requires looping through a huge Array and doing a lot of computations. Then we may have other computed properties that in turn depend on **A**. Without caching, we would be executing **A**’s getter many more times than necessary! In cases where you do not want caching, use a method instead.
112
+
80
113
### Computed vs Watched Property
81
114
82
115
Vue does provide a more generic way to observe and react to data changes on a Vue instance: **watch properties**. When you have some data that needs to change based on some other data, it is tempting to overuse `watch` - especially if you are coming from an AngularJS background. However, it is often a better idea to use a computed property rather than an imperative `watch` callback. Consider this example:
@@ -148,8 +181,6 @@ computed: {
148
181
149
182
Now when you run `vm.fullName = 'John Doe'`, the setter will be invoked and `vm.firstName` and `vm.lastName` will be updated accordingly.
150
183
151
-
The technical details behind how computed properties are updated are discussed in [another section](reactivity.html#Inside-Computed-Properties) dedicated to the reactivity system.
152
-
153
184
## Watchers
154
185
155
186
While computed properties are more appropriate in most cases, there are times when a custom watcher is necessary. That's why Vue provides a more generic way to react to data changes through the `watch` option. This is most useful when you want to perform asynchronous or expensive operations in response to changing data.
@@ -270,3 +301,5 @@ var watchExampleVM = new Vue({
270
301
{% endraw %}
271
302
272
303
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.
304
+
305
+
In addition to the `watch` option, you can also use the imperative [vm.$watch API](/api/#vm-watch).
Copy file name to clipboardExpand all lines: src/guide/reactivity.md
-38Lines changed: 0 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -135,41 +135,3 @@ Vue.component('example', {
135
135
}
136
136
})
137
137
```
138
-
139
-
## Inside Computed Properties
140
-
141
-
It should be noted that Vue's computed properties are **not** simple getters. Each computed property keeps track of its own reactive dependencies. When a computed property is evaluated, Vue updates its dependency list and caches the returned value. The cached value is only invalidated when one of the tracked dependencies have changed. Therefore, as long as the dependencies did not change, accessing the computed property will directly return the cached value instead of calling the getter.
142
-
143
-
Why do we need caching? Imagine we have an expensive computed property **A**, which requires looping through a huge Array and doing a lot of computations. Then we may have other computed properties that in turn depend on **A**. Without caching, we would be executing **A**’s getter many more times than necessary!
144
-
145
-
Because of computed property caching, the getter function is not always called when you access a computed property. Consider the following example:
146
-
147
-
```js
148
-
var vm =newVue({
149
-
data: {
150
-
message:'hi'
151
-
},
152
-
computed: {
153
-
example:function () {
154
-
returnDate.now() +this.message
155
-
}
156
-
}
157
-
})
158
-
```
159
-
160
-
The computed property `example` has only one dependency: `vm.message`. `Date.now()` is **not** a reactive dependency, because it has nothing to do with Vue's data observation system. Therefore, when you programmatically access `vm.example`, you will find the timestamp remains the same unless `vm.message` triggers a re-evaluation.
161
-
162
-
In some use cases, you may want to preserve the simple getter-like behavior, where every time you access `vm.example` it simply calls the getter again. You can do that by turning off caching for a specific computed property:
163
-
164
-
```js
165
-
computed: {
166
-
example: {
167
-
cache:false,
168
-
get:function () {
169
-
returnDate.now() +this.message
170
-
}
171
-
}
172
-
}
173
-
```
174
-
175
-
Now, every time you access `vm.example`, the timestamp will be up-to-date. **However, note this only affects programmatic access inside JavaScript; data-bindings are still dependency-driven.** When you bind to a computed property in the template as `{% raw %}{{ example }}{% endraw %}`, the DOM will only be updated when a reactive dependency has changed.
0 commit comments