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
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.
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.
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.
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**: このディレクティブを所有する ViewModel
52
+
-**expression**: 引数とフィルタ以外のバインディングの expression
53
+
-**arg**: 引数(もしある場合)
54
+
-**raw**: 元のパースされる前の expression
55
+
-**name**: prefix 無しのディレクティブの名前
56
56
57
-
<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:
59
+
いくつかのプロパティを使用したカスタムディレクティブの例:
60
60
61
61
```html
62
62
<divid="demo"v-demo="LightSlateGray : msg"></div>
@@ -85,7 +85,7 @@ var demo = new Vue({
85
85
})
86
86
```
87
87
88
-
**Result**
88
+
**結果**
89
89
90
90
<divid="demo"v-demo="LightSlateGray : msg"></div>
91
91
<script>
@@ -111,11 +111,11 @@ var demo = new Vue({
111
111
})
112
112
</script>
113
113
114
-
## Literal Directives
114
+
## リテラルディレクティブ
115
115
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.
However, in the case that the literal directive contains mustache tags, the behavior is as follows:
140
+
- もし `update` function が提供されていない場合、 mustache 表現は一度だけ評価され、 `this.expression` に割り当てられます。データ監視は行われません。
136
141
137
-
-The directive instance will have a flag `this._isDynamicLiteral` set to `true`;
142
+
-もし `update` function が提供される場合、ディレクティブはその expression に対するデータ監視をセットアップし、評価された結果が変更される度に `update` が呼ばれます。
138
143
139
-
- If no `update` function is provided, the mustache expression will be evaluated only once and assigned to `this.expression`. No data observation happens.
144
+
## 双方向ディレクティブ
140
145
141
-
- 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:
146
149
147
150
```js
148
151
Vue.directive('example', {
149
152
twoWay:true,
150
153
bind:function () {
151
154
this.handler=function () {
152
-
//set data back to the vm.
153
-
//If the directive is bound as v-example="a.b.c",
154
-
//this will attempt to set `vm.a.b.c` with the
155
-
//given value.
155
+
//vm にデータをセットします
156
+
//もしディレクティブが v-example="a.b.c" と紐付いている場合,
157
+
//与えられた値を `vm.a.b.c` に
158
+
//セットしようと試みます
156
159
this.set(this.el.value)
157
160
}.bind(this)
158
161
this.el.addEventListener('input', this.handler)
@@ -163,9 +166,9 @@ Vue.directive('example', {
163
166
})
164
167
```
165
168
166
-
## Inline Statements
169
+
## インラインステートメント
167
170
168
-
Passing in `acceptStatement:true`enables your custom directive to accept inline statements like `v-on`does:
@@ -175,18 +178,18 @@ Passing in `acceptStatement:true` enables your custom directive to accept inline
175
178
Vue.directive('my-directive', {
176
179
acceptStatement:true,
177
180
update:function (fn) {
178
-
//the passed in value is a function which when called,
179
-
//will execute the "a++" statement in the owner vm's
180
-
//scope.
181
+
//呼び出される際に渡される値は function です
182
+
//function は "a++" ステートメントを
183
+
//所有者の vm のスコープで実行します
181
184
}
182
185
})
183
186
```
184
187
185
-
Use this wisely though, because in general you want to avoid side-effects in your templates.
188
+
ただし、テンプレート内のサイドエフェクトを避けるためにも、賢く使いましょう。
186
189
187
-
## Deep Observation
190
+
## ディープ監視
188
191
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.
@@ -196,16 +199,16 @@ If your custom directive is expected to be used on an Object, and it needs to tr
196
199
Vue.directive('my-directive', {
197
200
deep:true,
198
201
update:function (obj) {
199
-
//will be called when nested properties in `obj`
200
-
//changes.
202
+
// `obj` の中のネストされたプロパティが
203
+
//変更された時に呼ばれる
201
204
}
202
205
})
203
206
```
204
207
205
-
## Directive Priority
208
+
## ディレクティブの優先度
206
209
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.
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.
Similar to custom directives, you can register a custom filter with the global `Vue.filter()`method, passing in a **filterID**and a **filter function**. The filter function takes a value as the argument and returns the transformed value:
@@ -18,7 +18,7 @@ Vue.filter('reverse', function (value) {
18
18
<spanv-text="message | reverse"></span>
19
19
```
20
20
21
-
The filter function also receives any inline arguments:
21
+
フィルタ関数はインラインの引数を受け取ることもできます:
22
22
23
23
```js
24
24
Vue.filter('wrap', function (value, begin, end) {
@@ -31,34 +31,34 @@ Vue.filter('wrap', function (value, begin, end) {
31
31
<spanv-text="message | wrap before after"></span>
32
32
```
33
33
34
-
## Two-way Filters
34
+
## 双方向フィルタ
35
35
36
-
Up till now we have used filters to transform values coming from the model and before displaying them in the view. But it is also possible to define a filter that transforms the value before it is written back to the model from the view (input elements):
When a filter is invoked, its `this`context is set to the Vue instance that is invoking it. This allows it to output dynamic results based on the state of the owner Vue instance.
// `this` points to the Vue instance invoking the filter
61
+
// `this` はフィルタを呼び出す Vue インスタンスを指します
62
62
return value +this[key]
63
63
})
64
64
```
@@ -67,8 +67,8 @@ Vue.filter('concat', function (value, key) {
67
67
<span>{{msg | concat userInput}}</span>
68
68
```
69
69
70
-
For this simple example above, you can achieve the same result with just an expression, but for more complicated procedures that need more than one statements, you need to put them either in a computed property or a custom filter.
The built-in `filterBy`and`orderBy`filters are both filters that perform non-trivial work on the Array being passed in and relies on the current state of the owner Vue instance.
0 commit comments