Skip to content

Commit 053f1c6

Browse files
committed
Merge pull request #19 from XfLoops/lang-zh
自定义指令
2 parents b1c02aa + 8fe0327 commit 053f1c6

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

source/guide/custom-directive.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ type: guide
33
order: 9
44
---
55

6-
## The Basics
6+
## 基础
77

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):
8+
Vue.js允许你注册自定义指令,实质上是让你教Vue一些新技巧:怎样将数据的变化映射成DOM的行为。你可以使用`Vue.directive(id, definition) `的方法传入**指令名称****定义对象**来注册一个全局自定义指令。定义对象要能提供一些钩子函数(全都可选):
99

10-
- **bind**: called only once, when the directive is first bound to the element.
11-
- **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.
12-
- **unbind**: called only once, when the directive is unbound from the element.
10+
- **bind**:仅调用一次,当指令第一次绑定元素的时候。
11+
- **update**: 第一次调用是在 bind之后,用的是初始值;以后每当绑定的值发生变化就会被调用,新值与旧值作为参数。
12+
- **unbind**:仅调用一次,当指令解绑元素的时候。
1313

1414
**Example**
1515

@@ -31,32 +31,32 @@ Vue.directive('my-directive', {
3131
})
3232
```
3333

34-
Once registered, you can use it in Vue.js templates like this (you need to add the Vue.js prefix to it):
34+
一旦注册好自定义指令,你就可以在Vue.js模板中像这样来使用它(你需要提前加上Vue.js):
3535

3636
``` html
3737
<div v-my-directive="someValue"></div>
3838
```
3939

40-
When you only need the `update` function, you can pass in a single function instead of the definition object:
40+
当你只用到`update`函数,你就可以只传入一个函数,而不用传定义对象:
4141

4242
``` js
4343
Vue.directive('my-directive', function (value) {
4444
// this function will be used as update()
4545
})
4646
```
4747

48-
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:
48+
所有的钩子函数会被复制到实际的**指令对象**中,而指令对象就是访问这些函数时的 `this`上下文环境。指令对象有一些有用的公开属性:
4949

50-
- **el**: the element the directive is bound to.
51-
- **vm**: the context ViewModel that owns this directive.
52-
- **expression**: the expression of the binding, excluding arguments and filters.
53-
- **arg**: the argument, if present.
54-
- **raw**: the raw, unparsed expression.
55-
- **name**: the name of the directive, without the prefix.
50+
- **el**: 指令绑定的元素
51+
- **vm**: 拥有该指令的上下文视图模型
52+
- **expression**: 绑定的表达式,不包括参数和过滤器
53+
- **arg**: 当前参数
54+
- **raw**: 未被解析的原始表达式
55+
- **name**: 不带前缀的指令名
5656

57-
<p class="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>
57+
<p class="tip"> 你应该把所有的这些属性看成是只读的,并且限制它们发生改变。你也可以给指令对象附加自定义的属性,但是注意不要重写已有的内部属性。
5858

59-
An example of a custom directive using some of these properties:
59+
下面是自定义指令使用其中一些属性的例子:
6060

6161
``` html
6262
<div id="demo" v-demo="LightSlateGray : msg"></div>
@@ -111,9 +111,10 @@ var demo = new Vue({
111111
})
112112
</script>
113113

114-
## Literal Directives
114+
## 直接指令
115115

116-
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.
116+
如果在创建自定义指令的时候传入 `isLiteral: true` ,那么属性值就会被看成直接字符串,并被指定为该指令的`表达式`。指令不会试图建
117+
立数据监视。
117118

118119
Example:
119120

@@ -130,19 +131,20 @@ Vue.directive('literal-dir', {
130131
})
131132
```
132133

133-
### Dynamic Literal
134+
### 动态直接量
134135

135-
However, in the case that the literal directive contains mustache tags, the behavior is as follows:
136+
然而,在直接指令含有mustache标签的情形下,指令的行为如下所示:
136137

137-
- The directive instance will have a flag `this._isDynamicLiteral` set to `true`;
138+
- 指令实例会有一个标记,`this._isDynamicLiteral` 被设为 `true`
138139

139-
- If no `update` function is provided, the mustache expression will be evaluated only once and assigned to `this.expression`. No data observation happens.
140+
- 如果没有提供 `update` 函数,mustache表达式只会被计算一次,并将值赋给 `this.expression` 。不会对表达式进行数据监视。
140141

141-
- If an `update` function is provided, the directive **will** setup data observation for that expression and call `update` when the evaluated result changes.
142+
- 如果提供了 `update` 函数,指令****会为表达式建立一个数据监视,并且在计算结果变化的时候调用 `update`
142143

143-
## Two-way Directives
144144

145-
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:
145+
## 双向指令
146+
147+
如果你的指令想向 Vue 实例写回数据,你需要传入 `twoWay: true` 。该选项允许在指令中使用 `this.set(value)`
146148

147149
``` js
148150
Vue.directive('example', {
@@ -163,9 +165,9 @@ Vue.directive('example', {
163165
})
164166
```
165167

166-
## Inline Statements
168+
## 内联声明
167169

168-
Passing in `acceptStatement:true` enables your custom directive to accept inline statements like `v-on` does:
170+
传入 `acceptStatement:true` 可以让自定义指令像 `v-on` 一样接受内联声明:
169171

170172
``` html
171173
<div v-my-directive="a++"></div>
@@ -182,11 +184,11 @@ Vue.directive('my-directive', {
182184
})
183185
```
184186

185-
Use this wisely though, because in general you want to avoid side-effects in your templates.
187+
但是要聪明地使用它,因为通常情况下你是想避免它在你的模板中产生副作用。
186188

187-
## Deep Observation
189+
## 深度监视
188190

189-
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.
191+
如果你希望在一个 `Object` 上使用自定义指令,并且当对象的内嵌属性发生变化时触发指令的 `update` 函数,那么你就要在指令的定义中传入 `deep: true`
190192

191193
``` html
192194
<div v-my-directive="obj"></div>
@@ -202,10 +204,10 @@ Vue.directive('my-directive', {
202204
})
203205
```
204206

205-
## Directive Priority
207+
## 指令优先级
206208

207-
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.
209+
你可以选择给指令提供一个优先级数(默认是0)。同一个元素上优先级越高的指令会比其他的指令处理得早一些。优先级一样的指令会按照其在元素属性列表中出现的顺序依次处理,但是不能保证这个顺序在不同的浏览器中是一致的。
208210

209-
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.
211+
你可以去[API reference](../api/directives.html)看一些内置指令的优先级。另外,`v-repeat`, `v-if` 以及 `v-component` 被视为“终端指令”,它们在编译过程中始终拥有最高的优先级。
210212

211-
Next, we'll see how to [write a custom filter](../guide/custom-filter.html).
213+
下面,我们来看怎样写一个[自定义过滤器](../guide/custom-filter.html)

0 commit comments

Comments
 (0)