Skip to content

Commit 79112bd

Browse files
massa142kazupon
authored andcommitted
translate custom directives (vuejs#188)
1 parent 1a30041 commit 79112bd

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

src/guide/custom-directive.md

+40-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
2-
title: Custom Directives
2+
title: カスタムディレクティブ
33
type: guide
44
order: 16
55
---
66

7-
## Intro
7+
## 基本
88

9-
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Note that in Vue 2.0, the primary form of code reuse and abstraction is components - however there may be cases where you just need some low-level DOM access on plain elements, and this is where custom directives would still be useful. An example would be focusing on an input element, like this one:
9+
コアで出荷されたディレクティブのデフォルトセットに加えて (`v-model``v-show`)、カスタムディレクティブ (custom directive) を登録することができます。
10+
Vue 2.0 では、コードの再利用と抽象化における基本の形はコンポーネントです。 - しかしながら、通常の要素で低レベル DOM にアクセスしなければならないケースがあるかもしれません。こういった場面では、カスタムディレクティブが役立つでしょう。
11+
ひとつの例として、以下のような input 要素へのフォーカスが挙げられます:
1012

1113
{% raw %}
1214
<div id="simplest-directive-example" class="demo">
@@ -24,69 +26,71 @@ new Vue({
2426
</script>
2527
{% endraw %}
2628

27-
When the page loads, that element gains focus. In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Now let's build the directive that accomplishes this:
29+
ページを読み込むと、この要素はフォーカスを手に入れます。実際、このページに訪れてから他のところをクリックしていなければ、この input にフォーカスが当たっているでしょう。
30+
さあ、これを実現させるディレクティブを作りましょう。
2831

2932
``` js
30-
// Register a global custom directive called v-focus
33+
// v-focus というグローバルカスタムディレクティブを登録します
3134
Vue.directive('focus', {
32-
// When the bound element is inserted into the DOM...
35+
// ひも付いている要素が DOM に挿入される時...
3336
inserted: function (el) {
34-
// Focus the element
37+
// 要素にフォーカスを当てる
3538
el.focus()
3639
}
3740
})
3841
```
3942

40-
If you want to register a directive locally instead, components also accept a `directives` option:
43+
代わりにローカルディレクティブに登録したいのであれば、コンポーネントの `directives` オプションで登録できます:
4144

4245
``` js
4346
directives: {
4447
focus: {
45-
// directive definition
48+
// ディレクティブ定義
4649
}
4750
}
4851
```
4952

50-
Then in a template, you can use the new `v-focus` attribute on any element, like this:
53+
これでテンプレートで、以下のような新しい `v-focus` 属性が使えるようになります。
5154

5255
``` html
5356
<input v-focus>
5457
```
5558

56-
## Hook Functions
5759

58-
A directive definition object can provide several hook functions (all optional):
60+
### フック関数
5961

60-
- `bind`: called only once, when the directive is first bound to the element. This is where you can do one-time setup work.
62+
directive definition object はいくつかのフック関数(全て任意)を提供します:
6163

62-
- `inserted`: called when the bound element has been inserted into its parent node (this only guarantees parent node presence, not necessarily in-document).
64+
- `bind`: ディレクティブが初めて対象の要素にひも付いた時に一度だけ呼ばれます。ここで一回だけ実行するセットアップ処理を行えます。
6365

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).
66+
- `inserted`: ひも付いている要素が親 Node に挿入された時に呼ばれます。(これは親 Node が存在していることのみ保証します。必ずしも in-document ではありません)
6567

66-
- `componentUpdated`: called after the containing component has completed an update cycle.
68+
- `update`: 含んでいるコンポーネントが更新された後に呼ばれます。__しかし、おそらく子コンポーネントが更新される前でしょう。__ ディレクティブの値が変化してもしなくても、バインディングされている値と以前の値との比較によって不要な更新を回避することができます。(フック引数に関しては下記を参照してください)
6769

68-
- `unbind`: called only once, when the directive is unbound from the element.
70+
- `componentUpdated`: 含んでいるコンポーネント __と子コンポーネント__ が更新された後に呼ばれます。
6971

70-
We'll explore the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `oldVnode`) in the next section.
72+
- `unbind`: ディレクティブがひも付いている要素から取り除かれた時に一度だけ呼ばれます。
7173

72-
## Directive Hook Arguments
74+
次のセクションで、これらのフックに渡す引数(すなわち `el`, `binging`, `vnode`, `oldVnode`)を見ていきましょう。
7375

74-
Directive hooks are passed these arguments:
76+
### ディレクティブフック引数
7577

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](/api/#VNode-Interface) for full details.
85-
- **oldVnode**: The previous virtual node, only available in the `update` and `componentUpdated` hooks.
78+
ディレクティブフックには以下の引数が渡せます:
8679

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>
80+
- **el**: ディレクティブがひも付く要素。DOMを直接操作するために使用できます。
81+
- **binding**: 以下のプロパティを含んでいるオブジェクト。
82+
- **name**: `v-` 接頭辞 (prefix) 無しのディレクティブ名。
83+
- **value**: ディレクティブに渡される値。例えば `v-my-directive="1 + 1"` では、 value は `2` です.
84+
- **oldValue**: `update``componentUpdated` においてのみ利用できる以前の値。値が変化したかどうかに関わらず利用できます。
85+
- **expression**: 文字列としてのバインディング式。例えば `v-my-directive="1 + 1"` では、expression は `"1 + 1"` です。
86+
- **arg**: もしあれば、ディレクティブに渡される引数。例えば `v-my-directive:foo` では、arg は `"foo"` です。
87+
- **modifiers**: もしあれば、修飾子 (modifier) を含んでいるオブジェクト。例えば `v-my-directive.foo.bar` では、modifiers オブジェクトは `{ foo: true, bar: true }` です。
88+
- **vnode**: Vue のコンパイラによって生成される仮想 Node。さらに詳しくは [VNode API](/api/#VNode-Interface) を参照してください。
89+
- **oldVnode**: `update``componentUpdated` フックにおいてのみ利用できる以前の仮想 Node。
8890

89-
An example of a custom directive using some of these properties:
91+
<p class="tip">`el` を除いて、これらの全てのプロパティは読み込みのみ (read-only) で変更しないものとして扱わなくてはいけません。フックを超えてデータを共有する必要がある場合は, 要素の [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset) を通じて行うことが推奨されています。</p>
92+
93+
いくつかのプロパティを使用したカスタムディレクティブの例:
9094

9195
``` html
9296
<div id="hook-arguments-example" v-demo:hello.a.b="message"></div>
@@ -138,19 +142,19 @@ new Vue({
138142
</script>
139143
{% endraw %}
140144

141-
## Function Shorthand
145+
## 関数省略記法
142146

143-
In many cases, you may want the same behavior on `bind` and `update`, but don't care about the other hooks. For example:
147+
多くの場合、`bind` `update` には同じ振舞いが欲しいでしょうが、その他のフックに関しては気にかけません。例えば:
144148

145149
``` js
146150
Vue.directive('color-swatch', function (el, binding) {
147151
el.style.backgroundColor = binding.value
148152
})
149153
```
150154

151-
## Object Literals
155+
## オブジェクトリテラル
152156

153-
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
157+
あなたのディレクティブが複数の値を必要ならば、JavaScript オブジェクトリテラルも渡すことができます。ディレクティブは任意の妥当な JavaScript 式を取ることができるのを覚えておいてください:
154158

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

0 commit comments

Comments
 (0)