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/guide/custom-directive.md
+1-53Lines changed: 1 addition & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,10 @@ type: guide
3
3
order: 9
4
4
---
5
5
6
-
## The Basics
7
6
## 基础
8
-
Vue.js allows you to register custom directives, essentially enabling you to teach Vue new tricks on how to map data changes to DOM behavior. You can register a global custom directive with the `Vue.directive(id, definition)` method, passing in a **directive id** followed by a **definition object**. A definition object can provide several hook functions (all optional):
-**bind**: called only once, when the directive is first bound to the element.
13
-
-**update**: called for the first time immediately after `bind` with the initial value, then again whenever the binding value changes. The new value and the previous value are provided as the argument.
14
-
-**unbind**: called only once, when the directive is unbound from the element.
Once registered, you can use it in Vue.js templates like this (you need to add the Vue.js prefix to it):
41
-
42
34
一旦注册好自定义指令,你就可以在Vue.js模板中像这样来使用它(你需要提前加上Vue.js):
43
35
44
36
```html
45
37
<divv-my-directive="someValue"></div>
46
38
```
47
39
48
-
When you only need the `update` function, you can pass in a single function instead of the definition object:
49
-
50
40
当你只用到`update`函数,你就可以只传入一个函数,而不用传定义对象:
51
41
52
42
```js
@@ -55,30 +45,17 @@ Vue.directive('my-directive', function (value) {
55
45
})
56
46
```
57
47
58
-
All the hook functions will be copied into the actual **directive object**, which you can access inside these functions as their `this` context. The directive object exposes some useful properties:
-**vm**: the context ViewModel that owns this directive.
64
-
-**expression**: the expression of the binding, excluding arguments and filters.
65
-
-**arg**: the argument, if present.
66
-
-**raw**: the raw, unparsed expression.
67
-
-**name**: the name of the directive, without the prefix.
68
-
69
50
-**el**: 指令绑定的元素
70
51
-**vm**: 拥有该指令的上下文视图模型
71
52
-**expression**: 绑定的表达式,不包括参数和过滤器
72
53
-**arg**: 当前参数
73
54
-**raw**: 未被解析的原始表达式
74
55
-**name**: 不带前缀的指令名
75
56
76
-
<pclass="tip">You should treat all these properties as read-only and refrain from changing them. You can attach custom properties to the directive object too, but be careful not to accidentally overwrite existing internal ones.</p>
An example of a custom directive using some of these properties:
81
-
82
59
下面是自定义指令使用其中一些属性的例子:
83
60
84
61
```html
@@ -134,11 +111,8 @@ var demo = new Vue({
134
111
})
135
112
</script>
136
113
137
-
## Literal Directives
138
114
## 直接指令
139
115
140
-
If you pass in `isLiteral: true` when creating a custom directive, the attribute value will be taken as a literal string and assigned as that directive's `expression`. The directive will not attempt to setup data observation.
However, in the case that the literal directive contains mustache tags, the behavior is as follows:
164
-
165
136
然而,在直接指令含有mustache标签的情形下,指令的行为如下所示:
166
137
167
-
- The directive instance will have a flag `this._isDynamicLiteral` set to `true`;
168
-
169
138
- 指令实例会有一个标记,`this._isDynamicLiteral` 被设为 `true`
170
139
171
-
- If no `update` function is provided, the mustache expression will be evaluated only once and assigned to `this.expression`. No data observation happens.
- If an `update` function is provided, the directive **will** setup data observation for that expression and call `update` when the evaluated result changes.
If your directive expects to write data back to the Vue instance, you need to pass in `twoWay: true`. This option allows the use of `this.set(value)` inside the directive:
Use this wisely though, because in general you want to avoid side-effects in your templates.
228
-
229
187
但是要聪明地使用它,因为通常情况下你是想避免它在你的模板中产生副作用。
230
188
231
-
## Deep Observation
232
189
## 深度监视
233
190
234
-
If your custom directive is expected to be used on an Object, and it needs to trigger `update` when a nested property inside the object changes, you need to pass in `deep: true` in your directive definition.
You can optionally provide a priority number for your directive (defaults to 0). A directive with a higher priority will be processed earlier than other directives on the same element. Directives with the same priority will be processed in the order they appear in the element's attribute list, although that order is not guaranteed to be consistent in different browsers.
You can checkout the priorities for some built-in directives in the [API reference](../api/directives.html). Additionally, `v-repeat`, `v-if` and `v-component` are considered "terminal directives" and they always have the highest priority in the compilation process.
0 commit comments