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
You may have noticed we can achieve the same result by invoking a method in the expression:
85
+
你可能已经注意到我们可以通过调用表达式中的method来达到同样的效果:
86
86
87
87
```html
88
88
<p>Reversed message: "{{ reverseMessage() }}"</p>
@@ -97,9 +97,9 @@ methods: {
97
97
}
98
98
```
99
99
100
-
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.
This also means the following computed property will never update, because `Date.now()`is not a reactive dependency:
102
+
这也同样意味着如下计算属性将不会更新,因为 `Date.now()`并不会被依赖:
103
103
104
104
```js
105
105
computed: {
@@ -109,13 +109,13 @@ computed: {
109
109
}
110
110
```
111
111
112
-
In comparison, a method invocation will **always** run the function whenever a re-render happens.
112
+
相比而言,每当重新渲染的时候,method调用**总会**运行函数。
113
113
114
-
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.
0 commit comments