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: source/api/instance-methods.md
+38-34Lines changed: 38 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ order: 4
5
5
6
6
## Data
7
7
8
-
> You can observe data changes on a Vue instance. Note that all watch callbacks fire asynchronously. In addition, value changes are batched within an event loop. This means when a value changes multiple times within a single event loop, the callback will be fired only once with the latest value.
Watch an expression on the Vue instance for changes. The expression can be a single keypath or actual expressions:
17
+
监听在Vue实例中改变的一个表达式。表达式可以是一个单独的路径或者是一个实际的表达式:
18
18
19
19
```js
20
20
vm.$watch('a + b', function (newVal, oldVal) {
21
21
// do something
22
22
})
23
23
```
24
24
25
-
To also detect nested value changes inside Objects, you need to pass in `true` for the third `deep` argument. Note that you don't need to do so to listen for Array mutations.
25
+
也可以监视在对象中的属性,你需要为 `deep` 传递一个 `true` 来使用此功能 。
26
+
27
+
请注意你不可以这么监视用来数组中元素的变化。
26
28
27
29
```js
28
30
vm.$watch('someObject', callback, true)
29
31
vm.someObject.nestedValue=123
30
-
//callback is fired
32
+
//回调被触发
31
33
```
32
34
33
-
Passing in `true`for the fourth `immediate`argument will trigger the callback immediately with the current value of the expression:
Set a data value on the Vue instance given a valid keypath. If the path doesn't exist it will be created.
61
+
通过给 Vue 实例传递一个可用的路径来设置结果,如果路径不存在那么会被创建。
60
62
61
63
### vm.$add( keypath, value )
62
64
63
65
-**keypath**`String`
64
66
-**value**`*`
65
67
66
-
Add a root level property to the Vue instance (and also its `$data`). Due to the limitations of ES5, Vue cannot detect properties directly added to or deleted from an Object, so use this method and `vm.$delete` when you need to do so. Additionally, all observed objects are augmented with these two methods too.
Evaluate a piece of template string containing mustache interpolations. Note that this method simply performs string interpolation; attribute directives are not compiled.
> Each vm is also an event emitter. When you have multiple nested ViewModels, you can use the event system to communicate between them.
112
+
> 每个 vm 都是一个事件的emitter。当你有很多的视图模型时,你可以用这种事件系统去在他们之间沟通数据。
110
113
111
114
### vm.$dispatch( event, [args...] )
112
115
113
116
-**event**`String`
114
117
-**args...***optional*
115
118
116
-
Dispatch an event from the current vm that propagates all the way up to its `$root`. If a callback returns `false`, it will stop the propagation at its owner instance.
119
+
从当前的 vm 处理一个事件,这个 vm 一直传播到它的 `$root` 。如果其中一个回调返回了 `false` ,那么在它自己的实例中停止传播。
117
120
118
121
### vm.$broadcast( event, [args...] )
119
122
120
123
-**event**`String`
121
124
-**args...***optional*
122
125
123
-
Emit an event to all children vms of the current vm, which gets further broadcasted to their children all the way down. If a callback returns `false`, its owner instance will not broadcast the event any further.
126
+
为当前 vm 的所有子 vm 注册该事件,这个 vm 会一直传播到到他的子vm。当一个回调返回 `false` 。当一个回调返回了 `false`,那么它的所有者实例就不会广播这个事件了。
124
127
125
128
### vm.$emit( event, [args...] )
126
129
127
130
-**event**`String`
128
131
-**args...***optional*
129
132
130
-
Trigger an event on this vm only.
133
+
只在这个 vm 里面触发被传递的事件。
131
134
132
135
### vm.$on( event, callback )
133
136
134
137
-**event**`String`
135
138
-**callback**`Function`
136
139
137
-
Listen for an event on the current vm.
140
+
在当前 vm 中监听事件。
138
141
139
142
### vm.$once( event, callback )
140
143
141
144
-**event**`String`
142
145
-**callback**`Function`
143
146
144
-
Attach a one-time only listener for an event.
147
+
为一个事件注册一个一次性的监听器。
145
148
146
149
### vm.$off( [event, callback] )
147
150
148
151
-**event**`String`*optional*
149
152
-**callback**`Function`*optional*
150
153
151
-
If no arguments are given, stop listening for all events; if only the event is given, remove all callbacks for that event; if both event and callback are given, remove that specific callback only.
> All vm DOM manipulation methods work like their jQuery counterparts - except they also trigger Vue.js transitions if there are any declared on vm's `$el`. For more details on transitions see [Adding Transition Effects](../guide/transitions.html).
158
+
> 所有 vm 的操作 DOM 的方法都是类似于jQuery中相同功能一样 - 除了被声明为 vm 的 `$el` 会触发 Vue.js 的转换。对于转化的更多细节可以看 [Adding Transition Effects](../guide/transitions.html) 。
156
159
157
160
### vm.$appendTo( element|selector, [callback] )
158
161
159
162
-**element**`HTMLElement` | **selector**`String`
160
163
-**callback**`Function`*optional*
161
164
162
-
Append the vm's `$el`to target element. The argument can be either an element or a querySelector string.
If the Vue instance didn't get an `el`option at instantiation, you can manually call `$mount()`to assign an element to it and start the compilation. If no argument is provided, an empty `<div>`will be automatically created. Calling `$mount()`on an already mounted instance will have no effect. The method returns the instance itself so you can chain other instance methods after it.
Completely destroy a vm. Clean up its connections with other existing vms, unbind all its directives and remove its `$el` from the DOM. Also, all `$on`and`$watch`listeners will be automatically removed.
199
+
完全销毁一个 vm。清空它对其他存在的 vm 的连接,释放它的所有中间件并且从DOM中移除它的 `$el`。并且所有的 `$on`和`$watch`监听器也都会被自动移除。
197
200
198
201
### vm.$compile( element )
199
202
200
203
-**element**`HTMLElement`
201
204
202
-
Partially compile a piece of DOM (Element or DocumentFragment). The method returns a `decompile` function that tearsdown the directives created during the process. Note the decompile function does not remove the DOM. This method is exposed primarily for writing advanced custom directives.
205
+
局部编译一个 DOM (元素或者是文档的片段)。这个方法返回一个 `decompile` 函数会关闭所有过程中已经被创建的中间件。请注意 decompile 函数不会移除DOM,这个方法对于写入高级自定义中间件来说根本上就是暴露的(exposed)。
206
+
203
207
204
208
### vm.$addChild( [options, constructor] )
205
209
206
210
-**options**`Object`*optional*
207
211
-**constructor**`Function`*optional*
208
212
209
-
Adds a child instance to the current instance. The options object is the same in manually instantiating an instance. Optionally you can pass in a constructor created from `Vue.extend()`.
0 commit comments