Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.

Commit a591633

Browse files
authored
Merge pull request #201 from huzhengen/data-methods
docs: src/guide/data-methods.md
2 parents b0d4f58 + 8143ca5 commit a591633

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/guide/data-methods.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
<!-- TODO: translation -->
1+
# Data Property 和方法
22

3-
# Data Properties and Methods
3+
## Data Property
44

5-
## Data Properties
6-
7-
The `data` option for a component is a function. Vue calls this function as part of creating a new component instance. It should return an object, which Vue will then wrap in its reactivity system and store on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly via the component instance:
5+
组件的 `data` 选项是一个函数。Vue 在创建新组件实例的过程中调用此函数。它应该返回一个对象,然后 Vue 会通过响应性系统将其包裹起来,并以 `$data` 的形式存储在组件实例中。为方便起见,该对象的任何顶级 property 也直接通过组件实例暴露出来:
86

97
```js
108
const app = Vue.createApp({
@@ -18,24 +16,24 @@ const vm = app.mount('#app')
1816
console.log(vm.$data.count) // => 4
1917
console.log(vm.count) // => 4
2018

21-
// Assigning a value to vm.count will also update $data.count
19+
// 修改 vm.count 的值也会更新 $data.count
2220
vm.count = 5
2321
console.log(vm.$data.count) // => 5
2422

25-
// ... and vice-versa
23+
// 反之亦然
2624
vm.$data.count = 6
2725
console.log(vm.count) // => 6
2826
```
2927

30-
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available.
28+
这些实例 property 仅在实例首次创建时被添加,所以你需要确保它们都在 `data` 函数返回的对象中。必要时,要对尚未提供所需值的 property 使用 `null``undefined` 或其他占位的值。。
3129

32-
It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](reactivity.html).
30+
直接将不包含在 `data` 中的新 property 添加到组件实例是可行的。但由于该 property 不在背后的响应式 `$data` 对象内,所以 [Vue 的响应性系统](reactivity.html)不会自动跟踪它。
3331

34-
Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters.
32+
Vue 使用 `$` 前缀通过组件实例暴露自己的内置 API。它还为内部 property 保留 `_` 前缀。你应该避免使用这两个字符开头的的顶级 `data` property 名称。
3533

36-
## Methods
34+
## 方法
3735

38-
To add methods to a component instance we use the `methods` option. This should be an object containing the desired methods:
36+
我们用 `methods` 选项向组件实例添加方法,它应该是一个包含所需方法的对象:
3937

4038
```js
4139
const app = Vue.createApp({
@@ -44,7 +42,7 @@ const app = Vue.createApp({
4442
},
4543
methods: {
4644
increment() {
47-
// `this` will refer to the component instance
45+
// `this` 指向该组件实例
4846
this.count++
4947
}
5048
}
@@ -59,63 +57,63 @@ vm.increment()
5957
console.log(vm.count) // => 5
6058
```
6159

62-
Vue automatically binds the `this` value for `methods` so that it always refers to the component instance. This ensures that a method retains the correct `this` value if it's used as an event listener or callback. You should avoid using arrow functions when defining `methods`, as that prevents Vue from binding the appropriate `this` value.
60+
Vue 自动为 `methods` 绑定 `this`,以便于它始终指向组件实例。这将确保方法在用作事件监听或回调时保持正确的 `this` 指向。在定义 `methods` 时应避免使用箭头函数,因为这会阻止 Vue 绑定恰当的 `this` 指向。
6361

64-
Just like all other properties of the component instance, the `methods` are accessible from within the component's template. Inside a template they are most commonly used as event listeners:
62+
这些 `methods` 和组件实例的其它所有 property 一样可以在组件的模板中被访问。在模板中,它们通常被当做事件监听使用:
6563

6664
```html
6765
<button @click="increment">Up vote</button>
6866
```
6967

70-
In the example above, the method `increment` will be called when the `<button>` is clicked.
68+
在上面的例子中,点击 `<button>` 时,会调用 `increment` 方法。
7169

72-
It is also possible to call a method directly from a template. As we'll see shortly, it's usually better to use a [computed property](computed.html) instead. However, using a method can be useful in scenarios where computed properties aren't a viable option. You can call a method anywhere that a template supports JavaScript expressions:
70+
也可以直接从模板中调用方法。就像下一章节即将看到的,通常换做[计算属性](computed.html)会更好。但是,在计算属性不可行的情况下,使用方法可能会很有用。你可以在模板支持 JavaScript 表达式的任何地方调用方法:
7371

7472
```html
7573
<span :title="toTitleDate(date)">
7674
{{ formatDate(date) }}
7775
</span>
7876
```
7977

80-
If the methods `toTitleDate` or `formatDate` access any reactive data then it will be tracked as a rendering dependency, just as if it had been used in the template directly.
78+
如果 `toTitleDate` `formatDate` 访问任何响应式数据,则将其作为渲染依赖项进行跟踪,就像直接在模板中使用过一样。
8179

82-
Methods called from a template should not have any side effects, such as changing data or triggering asynchronous processes. If you find yourself tempted to do that you should probably use a [lifecycle hook](instance.html#lifecycle-hooks) instead.
80+
从模板调用的方法不应该有任何副作用,比如更改数据或触发异步进程。如果你想这么做,应该换做[生命周期钩子](instance.html#生命周期钩子)
8381

84-
### Debouncing and Throttling
82+
### 防抖和节流
8583

86-
Vue doesn't include built-in support for debouncing or throttling but it can be implemented using libraries such as [Lodash](https://lodash.com/).
84+
Vue 没有内置支持防抖和节流,但可以使用 [Lodash](https://lodash.com/) 等库来实现。
8785

88-
In cases where a component is only used once, the debouncing can be applied directly within `methods`:
86+
如果某个组件仅使用一次,可以在 `methods` 中直接应用防抖:
8987

9088
```html
9189
<script src="https://unpkg.com/lodash@4.17.20/lodash.min.js"></script>
9290
<script>
9391
Vue.createApp({
9492
methods: {
95-
// Debouncing with Lodash
93+
// Lodash 的防抖函数
9694
click: _.debounce(function() {
97-
// ... respond to click ...
95+
// ... 响应点击 ...
9896
}, 500)
9997
}
10098
}).mount('#app')
10199
</script>
102100
```
103101

104-
However, this approach is potentially problematic for components that are reused because they'll all share the same debounced function. To keep the component instances independent from each other, we can add the debounced function in the `created` lifecycle hook:
102+
但是,这种方法对于可复用组件有潜在的问题,因为它们都共享相同的防抖函数。为了使组件实例彼此独立,可以在生命周期钩子的 `created` 里添加该防抖函数:
105103

106104
```js
107105
app.component('save-button', {
108106
created() {
109-
// Debouncing with Lodash
107+
// Lodash 的防抖函数
110108
this.debouncedClick = _.debounce(this.click, 500)
111109
},
112110
unmounted() {
113-
// Cancel the timer when the component is removed
111+
// 移除组件时,取消定时器
114112
this.debouncedClick.cancel()
115113
},
116114
methods: {
117115
click() {
118-
// ... respond to click ...
116+
// ... 响应点击 ...
119117
}
120118
},
121119
template: `

0 commit comments

Comments
 (0)