Skip to content

Commit d153bf3

Browse files
authored
Update computed.md
1 parent f37dd71 commit d153bf3

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/guide/computed.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ order: 5
1414
</div>
1515
```
1616

17-
在这种情况下,模板不在简单和清晰。你不得不停留下意识到显示的是反向的`message`。这个问题在你不止一次反转message的时候变得更加糟糕
17+
在这种情况下,模板不再简单和清晰。在实现反向显示 `message` 之前,你应该确认它。这个问题在你不止一次反向显示 message 的时候变得更加糟糕
1818

1919
这就是为什么任何复杂逻辑,你都应当使用**计算属性**
2020

@@ -68,21 +68,21 @@ var vm = new Vue({
6868
{% endraw %}
6969

7070

71-
这里我们声明了一个计算属性 `reversedMessage`。我们提供的函数将用作属性 `vm.reversedMessage`的 getter。
71+
这里我们声明了一个计算属性 `reversedMessage` 。我们提供的函数将用作属性 `vm.reversedMessage` 的 getter
7272

7373
``` js
7474
console.log(vm.reversedMessage) // -> 'olleH'
7575
vm.message = 'Goodbye'
7676
console.log(vm.reversedMessage) // -> 'eybdooG'
7777
```
7878

79-
你可以打开浏览器的控制台,修改 vm`vm.reversedMessage` 的值始终取决于 `vm.message` 的值。
79+
你可以打开浏览器的控制台,修改 vm`vm.reversedMessage` 的值始终取决于 `vm.message` 的值。
8080

81-
你可以像绑定普通属性一样在模板中绑定计算属性。Vue 知道 `vm.reversedMessage` 依赖于 `vm.message`,因此当 `vm.message` 发生改变时,依赖于 `vm.reversedMessage` 的绑定也会更新。而且最妙的是我们是声明式地创建这种依赖关系:计算属性的 getter 是干净无副作用的,因此也是易于测试和理解的。
81+
你可以像绑定普通属性一样在模板中绑定计算属性。 Vue 知道 `vm.reversedMessage` 依赖于 `vm.message` ,因此当 `vm.message` 发生改变时,依赖于 `vm.reversedMessage` 的绑定也会更新。而且最妙的是我们是声明式地创建这种依赖关系:计算属性的 getter 是干净无副作用的,因此也是易于测试和理解的。
8282

83-
### Computed Caching vs Methods
83+
### 计算缓存 vs Methods
8484

85-
You may have noticed we can achieve the same result by invoking a method in the expression:
85+
你可能已经注意到我们可以通过调用表达式中的method来达到同样的效果:
8686

8787
``` html
8888
<p>Reversed message: "{{ reverseMessage() }}"</p>
@@ -97,9 +97,9 @@ methods: {
9797
}
9898
```
9999

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.
100+
不经过计算属性,我们可以通过定义一个method来替代它。对于最终的结果,两种方式确实是相同的。然而,不同的是**计算缓存基于它的相关依赖**。计算属性只有在它的相关依赖发生改变时才会重新评估。这就意味着如果 `message` 没有发生改变,多次访问 `reversedMessage` 计算属性会立即返回之前的结果,而不必运行函数。
101101

102-
This also means the following computed property will never update, because `Date.now()` is not a reactive dependency:
102+
这也同样意味着如下计算属性将不会更新,因为 `Date.now()` 并不会被依赖:
103103

104104
``` js
105105
computed: {
@@ -109,13 +109,13 @@ computed: {
109109
}
110110
```
111111

112-
In comparison, a method invocation will **always** run the function whenever a re-render happens.
112+
相比而言,每当重新渲染的时候,method调用**总会**运行函数。
113113

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.
114+
我们为什么需要缓存?假设我们有一个重要的计算属性 **A** ,这个计算属性需要一个巨大的数组遍历和做大量的计算。然后我们可能有其他的计算属性依赖于 **A** 。如果没有缓存,我们将执行多次 **A**getter ,然而这都是不必要的!如果你不希望有缓存,请用method替代。
115115

116-
### 计算属性 vs watch
116+
### 计算属性 vs Watched Property
117117

118-
Vue.js 提供了一个方法 `$watch`,它用于观察 Vue 实例上的数据变动。当一些数据需要根据其它数据变化时, `$watch` 很诱人 —— 特别是如果你来自 AngularJS。不过,通常更好的办法是使用计算属性而不是一个命令式的 `$watch` 回调。考虑下面例子
118+
Vue.js 提供了一个方法 `$watch` ,它用于观察 Vue 实例上的数据变动。当一些数据需要根据其它数据变化时, `$watch` 很诱人 —— 特别是如果你来自 AngularJS 。不过,通常更好的办法是使用计算属性而不是一个命令式的 `$watch` 回调。思考下面例子
119119

120120
``` html
121121
<div id="demo">{{ fullName }}</div>
@@ -161,7 +161,7 @@ var vm = new Vue({
161161

162162
### 计算 setter
163163

164-
计算属性默认只是 getter,不过在需要时你也可以提供一个 setter:
164+
计算属性默认只是 getter ,不过在需要时你也可以提供一个 setter
165165

166166
``` js
167167
// ...
@@ -182,13 +182,13 @@ computed: {
182182
// ...
183183
```
184184

185-
现在在调用 `vm.fullName = 'John Doe'` 时,setter 会被调用,`vm.firstName``vm.lastName` 也会有相应更新。
185+
现在在调用 `vm.fullName = 'John Doe'` 时, setter 会被调用, `vm.firstName``vm.lastName` 也会有相应更新。
186186

187187
关于计算属性背后的原理和技术细节详见[计算属性的奥秘](http://rc.vuejs.org/guide/reactivity.html#Inside-Computed-Properties)
188188

189189
## 观察 Watchers
190190

191-
当计算属性使用在大多数场景下时,有时候也很需要一个自定义的数watcher.这是为什么Vue提供一个更通用的方法通过`watch`的设置,来反应数据的变化。在你想要执行异步操作或需要响应不断变化昂贵的数据操作时这很有用。
191+
当计算属性使用在大多数场景下时,有时候也很需要一个自定义的 watcher 。这是为什么 Vue 提供一个更通用的方法通过 `watch` 的设置,来反应数据的变化。在你想要执行异步操作或需要响应不断变化昂贵的数据操作时这很有用。
192192

193193
例如:
194194

@@ -217,20 +217,18 @@ var watchExampleVM = new Vue({
217217
answer: 'I cannot give you an answer until you ask a question!'
218218
},
219219
watch: {
220-
// whenever question changes, this function will run
220+
  // 如果 question 发生改变,这个函数就会运行
221221
question: function (newQuestion) {
222222
this.answer = 'Waiting for you to stop typing...'
223223
this.getAnswer()
224224
}
225225
},
226226
methods: {
227-
// _.debounce is a function provided by lodash to limit how
228-
// often a particularly expensive operation can be run.
229-
// In this case, we want to limit how often we access
230-
// yesno.wtf/api, waiting until the user has completely
231-
// finished typing before making the ajax request. To learn
232-
// more about the _.debounce function (and its cousin
233-
// _.throttle), visit: https://lodash.com/docs#debounce
227+
  // _.debounce 是一个通过 lodash 限制操作频率的函数。
228+
  // 在这个例子中,我们希望限制访问yesno.wtf/api的频率
229+
  // 一直等待直到用户发出ajax请求之前
230+
  // 学习更多关于 _.debounce function (and its cousin
231+
// _.throttle), 参考: https://lodash.com/docs#debounce
234232
getAnswer: _.debounce(
235233
function () {
236234
var vm = this
@@ -247,8 +245,7 @@ var watchExampleVM = new Vue({
247245
vm.answer = 'Error! Could not reach the API. ' + error
248246
})
249247
},
250-
// This is the number of milliseconds we wait for the
251-
// user to stop typing.
248+
// 这是我们为用户停止输入等待的毫秒数
252249
500
253250
)
254251
}
@@ -277,7 +274,7 @@ var watchExampleVM = new Vue({
277274
answer: 'I cannot give you an answer until you ask a question!'
278275
},
279276
watch: {
280-
// whenever question changes, this function will run
277+
// 如果 question 发生改变,这个函数就会运行
281278
question: function (newQuestion) {
282279
this.answer = 'Waiting for you to stop typing...'
283280
this.getAnswer()
@@ -300,18 +297,17 @@ var watchExampleVM = new Vue({
300297
vm.answer = 'Error! Could not reach the API. ' + error
301298
})
302299
},
303-
// This is the number of milliseconds we wait for the
304-
// user to stop typing.
300+
// 这是我们为用户停止输入等待的毫秒数
305301
500
306302
)
307303
}
308304
})
309305
</script>
310306
{% endraw %}
311307

312-
在这个示例中,使用`watch`的设置允许我们执行异步操作(访问一个接口),限制我们多久执行该操作,并在我们获取最终结果时立刻设置状态。这是计算属性无法做到的。
308+
在这个示例中,使用 `watch` 的设置允许我们执行异步操作(访问一个接口),限制我们多久执行该操作,并在我们获取最终结果时立刻设置状态。这是计算属性无法做到的。
313309

314-
关于`watch` 选项,可看API文档[vm.$watch API](/api/#vm-watch)
310+
关于 `watch` 选项,可看API文档[vm.$watch API](/api/#vm-watch)
315311

316312
***
317313

0 commit comments

Comments
 (0)