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
{{ message }}
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: src/guide/data-methods.md
+26-28Lines changed: 26 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,8 @@
1
-
<!-- TODO: translation -->
1
+
# Data Property 和方法
2
2
3
-
# Data Properties and Methods
3
+
##Data Property
4
4
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:
//Assigning a value to vm.count will also update $data.count
19
+
//修改 vm.count 的值也会更新 $data.count
22
20
vm.count=5
23
21
console.log(vm.$data.count) // => 5
24
22
25
-
//... and vice-versa
23
+
//反之亦然
26
24
vm.$data.count=6
27
25
console.log(vm.count) // => 6
28
26
```
29
27
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.
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).
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.
To add methods to a component instance we use the `methods`option. This should be an object containing the desired methods:
36
+
我们用 `methods`选项向组件实例添加方法,它应该是一个包含所需方法的对象:
39
37
40
38
```js
41
39
constapp=Vue.createApp({
@@ -44,7 +42,7 @@ const app = Vue.createApp({
44
42
},
45
43
methods: {
46
44
increment() {
47
-
// `this` will refer to the component instance
45
+
// `this` 指向该组件实例
48
46
this.count++
49
47
}
50
48
}
@@ -59,63 +57,63 @@ vm.increment()
59
57
console.log(vm.count) // => 5
60
58
```
61
59
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.
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:
In the example above, the method `increment` will be called when the `<button>`is clicked.
68
+
在上面的例子中,点击 `<button>`时,会调用 `increment` 方法。
71
69
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:
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.
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.
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:
0 commit comments