Skip to content

Commit b1857bb

Browse files
authored
Merge pull request volksbright#30 from harrytospring/2.0-cn
翻译
2 parents 563f48b + a0c4e47 commit b1857bb

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

src/guide/custom-directive.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ order: 16
66

77
## 简介
88

9-
除了默认设置的核心指令(`v-model``v-show`),Vue也允许注册自定义指令。注意,在vue2.0里面,代码复用的主要形式和抽象是组件——然而可能有情况下,你只需要一些低级的DOM访问纯元素,这就是自定义指令仍将是有用的。下面这个例子聚集一个input元素,像这样:
9+
除了默认设置的核心指令(`v-model``v-show`),Vue也允许注册自定义指令。注意,在vue2.0里面,代码复用的主要形式和抽象是组件——然而,有的情况下,你仍然需要对纯DOM元素进行底层操作,这时候就会用到自定义指令。下面这个例子聚焦一个input元素,像这样:
1010

1111
{% raw %}
1212
<div id="simplest-directive-example" class="demo">
@@ -24,10 +24,10 @@ new Vue({
2424
</script>
2525
{% endraw %}
2626

27-
页面加载时,元素将获得焦点。事实上,你访问后还没点击任何内容,input就被聚焦了。现在让我们完善这个指令:
27+
页面加载时,元素将获得焦点。事实上,你访问后还没点击任何内容,input就获得了焦点。现在让我们完善这个指令:
2828

2929
``` js
30-
// 注册一个全局自定义指令叫做 v-focus
30+
// 注册一个全局自定义指令 v-focus
3131
Vue.directive('focus', {
3232
// 当绑定元素插入到DOM中。
3333
inserted: function (el) {
@@ -42,7 +42,8 @@ Vue.directive('focus', {
4242
``` js
4343
directives: {
4444
focus: {
45-
// 指令的定义
45+
// 指令的定义---
46+
4647
}
4748
}
4849
```
@@ -55,38 +56,38 @@ directives: {
5556

5657
## 钩子函数
5758

58-
A directive definition object can provide several hook functions (all optional):
59+
指令定义函数提供了几个钩子函数(可选):
5960

60-
- `bind`: called only once, when the directive is first bound to the element. This is where you can do one-time setup work.
61+
- `bind`: 只调用一次, 指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作。
6162

62-
- `inserted`: called when the bound element has been inserted into its parent node (this only guarantees parent node presence, not necessarily in-document).
63+
- `inserted`: 被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document 中)。
6364

64-
- `update`: called whenever the bound element's containing component is updated. The directive's value may or may not have changed. You can skip unnecessary updates by comparing the binding's current and old values (see below on hook arguments).
65+
- `update`: 被绑定元素所在的模板更新时调用,而不论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新(详细的钩子函数参数见下)。
6566

66-
- `componentUpdated`: called after the containing component has completed an update cycle.
67+
- `componentUpdated`: 被绑定元素所在模板完成一次更新周期时调用。
6768

68-
- `unbind`: called only once, when the directive is unbound from the element.
69+
- `unbind`: 只调用一次, 指令与元素解绑时调用。
6970

70-
We'll explore the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `oldVnode`) in the next section.
71+
接下来我们来看一下钩子函数的参数 (包括 `el` `binding` `vnode``oldVnode`)
7172

72-
## Directive Hook Arguments
73+
## 钩子函数参数
7374

74-
Directive hooks are passed these arguments:
75+
钩子函数被赋予了以下参数:
7576

76-
- **el**: The element the directive is bound to. This can be used to directly manipulate the DOM.
77-
- **binding**: An object containing the following properties.
78-
- **name**: The name of the directive, without the `v-` prefix.
79-
- **value**: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be `2`.
80-
- **oldValue**: The previous value, only available in `update` and `componentUpdated`. It is available whether or not the value has changed.
81-
- **expression**: The expression of the binding as a string. For example in `v-my-directive="1 + 1"`, the expression would be `"1 + 1"`.
82-
- **arg**: The argument passed to the directive, if any. For example in `v-my-directive:foo`, the arg would be `"foo"`.
83-
- **modifiers**: An object containing modifiers, if any. For example in `v-my-directive.foo.bar`, the modifiers object would be `{ foo: true, bar: true }`.
84-
- **vnode**: The virtual node produced by Vue's compiler.<!--See the [VNode API]([!!TODO: Add link to the VNode API doc when it exists]) for full details.-->
85-
- **oldVnode**: The previous virtual node, only available in the `update` and `componentUpdated` hooks.
77+
- **el**: 指令所绑定的元素,可以用来直接操作 dom 。
78+
- **binding**: 一个对象,包含以下属性:
79+
- **name**: 指令名,不包括 `v-` 前缀。
80+
- **value**: 指令的绑定值, 例如: `v-my-directive="1 + 1"`, value 的值是 `2`
81+
- **oldValue**: 指令绑定的前一个值, 仅在 `update` `componentUpdated` 钩子中可用。无论值是否改变都可用。
82+
- **expression**: 绑定值的字符串形式。 例如 `v-my-directive="1 + 1"` expression 的值是 `"1 + 1"`
83+
- **arg**: 传给指令的参数。例如 `v-my-directive:foo`arg 的值是 `"foo"`
84+
- **modifiers**: 一个包含编辑器的对象。 例如: `v-my-directive.foo.bar`, 编辑器对象 modifiers的值是 `{ foo: true, bar: true }`.
85+
- **vnode**: Vue 生成的虚拟节点<!--参考 [VNode API]([!!TODO: Add link to the VNode API doc when it exists]) for full details.-->
86+
- **oldVnode**: 上一个虚拟节点, 仅在 `update` `componentUpdated` 钩子中可用。
8687

87-
<p class="tip">Apart from `el`, you should treat these arguments as read-only and never modify them. If you need to share information across hooks, it is recommended to do so through element's [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).</p>
88+
<p class="tip">除了 `el` 之外, 其它参数都应该是只读的,尽量不要修改他们。如果需要在钩子之间共享数据,建议通过元素的 [dataset] 来进行。(https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).</p>
8889

89-
An example of a custom directive using some of these properties:
90+
一个使用了这些参数的自定义钩子样例:
9091

9192
``` html
9293
<div id="hook-arguments-example" v-demo:hello.a.b="message"></div>
@@ -138,19 +139,18 @@ new Vue({
138139
</script>
139140
{% endraw %}
140141

141-
## Function Shorthand
142+
## 函数简写
142143

143-
In many cases, you may want the same behavior on `bind` and `update`, but don't care about the other hooks. For example:
144+
大多数情况下,只需要在 `bind` `update` 钩子中做重复动作,而不关心其它钩子函数。可以这样写:
144145

145146
``` js
146147
Vue.directive('color-swatch', function (el, binding) {
147148
el.style.backgroundColor = binding.value
148149
})
149150
```
151+
## 对象常量
150152

151-
## Object Literals
152-
153-
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
153+
如果自定义指令需要复杂的值,也可以传递 javascript 对象。要知道,指令函数能够接受所有类型的 javascript 常量。
154154

155155
``` html
156156
<div v-demo="{ color: 'white', text: 'hello!' }"></div>

src/guide/mixins.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ type: guide
44
order: 17
55
---
66

7-
## Basics
7+
## 基础
88

9-
Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be "mixed" into the component's own options.
9+
混合是一种灵活的分布式复用 Vue 组件的方式。混合对象可以包含任意组件选项。以混合方式使用组件时,所有混合选项将被混入该组件本身的选项。
1010

11-
Example:
11+
例子:
1212

1313
``` js
14-
// define a mixin object
14+
// 定义一个混合对象
1515
var myMixin = {
1616
created: function () {
1717
this.hello()
@@ -23,17 +23,17 @@ var myMixin = {
2323
}
2424
}
2525

26-
// define a component that uses this mixin
26+
// 定义一个使用混合对象的组件
2727
var Component = Vue.extend({
2828
mixins: [myMixin]
2929
})
3030

3131
var component = new Component() // -> "hello from mixin!"
3232
```
3333

34-
## Option Merging
34+
## 选项合并
3535

36-
When a mixin and the component itself contain overlapping options, they will be "merged" using appropriate strategies. For example, hook functions with the same name are merged into an array so that all of them will be called. In addition, mixin hooks will be called **before** the component's own hooks:
36+
当组件和混合对象含有同名选项时, 这些选项将以恰当的方式混合。比如,同名钩子函数将混合为一个数组,因此都将被调用。另外,混合对象的 钩子将在组件自身钩子 **之前** 调用 :
3737

3838
``` js
3939
var mixin = {
@@ -49,11 +49,11 @@ new Vue({
4949
}
5050
})
5151

52-
// -> "mixin hook called"
53-
// -> "component hook called"
52+
// -> "混合对象的钩子被调用"
53+
// -> "组件钩子被调用"
5454
```
5555

56-
Options that expect object values, for example `methods`, `components` and `directives`, will be merged into the same object. The component's options will take priority when there are conflicting keys in these objects:
56+
值为对象的选项, 例如 `methods`, `components` `directives`,将被混合为同一个对象。 两个对象键名冲突时,取组件对象的键值对。
5757

5858
``` js
5959
var mixin = {
@@ -84,14 +84,14 @@ vm.bar() // -> "bar"
8484
vm.conflicting() // -> "from self"
8585
```
8686

87-
Note that the same merge strategies are used in `Vue.extend()`.
87+
注意: `Vue.extend()` 也使用同样的策略进行合并。
8888

89-
## Global Mixin
89+
## 全局混合
9090

91-
You can also apply a mixin globally. Use caution! Once you apply a mixin globally, it will affect **every** Vue instance created afterwards. When used properly, this can be used to inject processing logic for custom options:
91+
也可以全局注册混合对象。 注意使用! 一旦使用全局混合对象,将会影响到 **所有** 之后创建的 Vue 实例。使用恰当时,可以为自定义对象注入处理逻辑。
9292

9393
``` js
94-
// inject a handler for `myOption` custom option
94+
// 为自定义的选项 'myOption' 注入一个处理器。
9595
Vue.mixin({
9696
created: function () {
9797
var myOption = this.$options.myOption
@@ -107,26 +107,26 @@ new Vue({
107107
// -> "hello!"
108108
```
109109

110-
<p class="tip">Use global mixins sparsely and carefully, because it affects every single Vue instance created, including third party components. In most cases, you should only use it for custom option handling like demonstrated in the example above. It's also a good idea to ship them as [Plugins](/guide/plugins.html) to avoid duplicate application.</p>
110+
<p class="tip">谨慎使用全局混合对象,因为会影响到每个单独创建的 Vue 实例(包括第三方模板)。大多数情况下,只应当应用于自定义选项,就像上面示例一样。 也可以将其用作 [Plugins](/guide/plugins.html) 以避免产生重复应用</p>
111111

112-
## Custom Option Merge Strategies
112+
## 自定义选项混合策略
113113

114-
When custom options are merged, they use the default strategy, which simply overwrites the existing value. If you want a custom option to be merged using custom logic, you need to attach a function to `Vue.config.optionMergeStrategies`:
114+
自定义选项将使用默认策略,即简单地覆盖已有值。 如果想让自定义选项以自定义逻辑混合,可以向 `Vue.config.optionMergeStrategies` 添加一个函数:
115115

116116
``` js
117117
Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) {
118118
// return mergedVal
119119
}
120120
```
121121

122-
For most object-based options, you can simply use the same strategy used by `methods`:
122+
对于大多数对象选项,可以使用 `methods` 的合并策略:
123123

124124
``` js
125125
var strategies = Vue.config.optionMergeStrategies
126126
strategies.myOption = strategies.methods
127127
```
128128

129-
A more advanced example can be found on [Vuex](https://github.com/vuejs/vuex)'s merging strategy:
129+
更多高级的例子可以在 [Vuex](https://github.com/vuejs/vuex) 的混合策略里找到:
130130

131131
``` js
132132
const merge = Vue.config.optionMergeStrategies.computed

0 commit comments

Comments
 (0)