Skip to content

Commit 05f8ab9

Browse files
authored
docs #26 Reusability & Composition > Custom Directives の翻訳 (#68)
* translate Reusability & Composition > Custom Directives * add colon where there is colon * use 束縛 instead of バインド * use Note to align other pages
1 parent c7e7f2e commit 05f8ab9

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/guide/custom-directive.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Custom Directives
1+
# カスタムディレクティブ
22

3-
## Intro
3+
## 基本
44

5-
In addition to the default set of directives shipped in core (like `v-model` or `v-show`), Vue also allows you to register your own custom directives. Note that in Vue, the primary form of code reuse and abstraction is components - however, there may be cases where you 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:
5+
Vue.js 本体で提供されているデフォルトのディレクティブ (`v-model` `v-show`) に加えて、独自のカスタムディレクティブ (custom directives) を登録することも可能です。Vue ではコードの再利用や抽象化の基本形はコンポーネントです。しかしながら、単純な要素への低レベルな DOM のアクセスが必要なケースがあるかもしれません。こういったケースにカスタムディレクティブが役に立つことでしょう。以下のような input 要素へのフォーカスが1つの例として挙げられます:
66

77
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="result" data-user="Vue" data-slug-hash="JjdxaJW" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Custom directives: basic example">
88
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/JjdxaJW">
@@ -11,68 +11,68 @@ In addition to the default set of directives shipped in core (like `v-model` or
1111
</p>
1212
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
1313

14-
When the page loads, that element gains focus (note: `autofocus` doesn't work on mobile Safari). In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Also, you can click on the `Rerun` button and input will be focused.
14+
ページを読み込むと、この要素にフォーカスが当たります (注意:`autofucus` はモバイルの Safari で動きません)。実際、このページに訪れてから他に何もクリックしなければ、上記の input 要素にフォーカスが当たります。また、`Rerun` ボタンをクリックしても、input 要素はフォーカスされます。
1515

16-
Now let's build the directive that accomplishes this:
16+
ここからこれを実現するディレクティブを実装しましょう:
1717

1818
```js
1919
const app = Vue.createApp({})
20-
// Register a global custom directive called `v-focus`
20+
// `v-focus` と呼ばれるグローバルカスタムディレクティブを登録します
2121
app.directive('focus', {
22-
// When the bound element is mounted into the DOM...
22+
// 束縛されている要素が DOM にマウントされると...
2323
mounted(el) {
24-
// Focus the element
24+
// その要素にフォーカスを当てる
2525
el.focus()
2626
}
2727
})
2828
```
2929

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

3232
```js
3333
directives: {
3434
focus: {
35-
// directive definition
35+
// ディレクティブの定義
3636
mounted(el) {
3737
el.focus()
3838
}
3939
}
4040
}
4141
```
4242

43-
Then in a template, you can use the new `v-focus` attribute on any element, like this:
43+
そしてテンプレートでは、新規登録した `v-focus` 属性をどの要素に対しても以下のように利用できます:
4444

4545
```html
4646
<input v-focus />
4747
```
4848

49-
## Hook Functions
49+
## フック関数
5050

51-
A directive definition object can provide several hook functions (all optional):
51+
ディレクティブの定義オブジェクトは、いくつかのフック関数を提供しています (全てオプション):
5252

53-
- `beforeMount`: called when the directive is first bound to the element and before parent component is mounted. This is where you can do one-time setup work.
53+
- `beforeMount`: ディレクティブが初めて要素に束縛された時、そして親コンポーネントがマウントされる前に呼ばれます。ここは1度だけ実行するセットアップ処理を行える場所です。
5454

55-
- `mounted`: called when the bound element's parent component is mounted.
55+
- `mounted`: 束縛された要素の親コンポーネントがマウントされた時に呼ばれます。
5656

57-
- `beforeUpdate`: called before the containing component's VNode is updated
57+
- `beforeUpdate`: 束縛された要素を含むコンポーネントの VNode が更新される前に呼ばれます。
5858

5959
:::tip Note
60-
We'll cover VNodes in more detail [later](render-function.html#the-virtual-dom-tree), when we discuss render functions.
60+
VNodes [後で](render-function.html#the-virtual-dom-tree)詳細に扱います。描画関数を説明する時です。
6161
:::
6262

63-
- `updated`: called after the containing component's VNode **and the VNodes of its children** have updated.
63+
- `updated`: 束縛された要素を含むコンポーネントの VNode **とその子コンポーネントの VNode** が更新された後に呼ばれます。
6464

65-
- `beforeUnmount`: called before the bound element's parent component is unmounted
65+
- `beforeUnmount`: 束縛された要素の親コンポーネントがアンマウントされる前に呼ばれます。
6666

67-
- `unmounted`: called only once, when the directive is unbound from the element and the parent component is unmounted.
67+
- `unmounted`: ディレクティブが要素から束縛を解かれた時、また親コンポーネントがアンマウントされた時に1度だけ呼ばれます。
6868

69-
You can check the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `prevVnode`) in [Custom Directive API](../api/application-api.html#directive)
69+
これらのフック関数に渡される引数 (すなわち、`el`, `binding`, `vnode`, `prevVnode`) は、[カスタムディレクティブ API](../api/application-api.html#directive) にて確認できます。
7070

71-
### Dynamic Directive Arguments
71+
### 動的なディレクティブ引数
7272

73-
Directive arguments can be dynamic. For example, in `v-mydirective:[argument]="value"`, the `argument` can be updated based on data properties in our component instance! This makes our custom directives flexible for use throughout our application.
73+
ディレクティブの引数は動的にできます。例えば、`v-mydirective:[argument]="value"` において、`argument` はコンポーネントインスタンスの data プロパティに基づいて更新されます! これにより、私たちのアプリケーション全体を通した利用に対して、カスタムディレクティブは柔軟になります。
7474

75-
Let's say you want to make a custom directive that allows you to pin elements to your page using fixed positioning. We could create a custom directive where the value updates the vertical positioning in pixels, like this:
75+
ページの固定位置に要素をピン留めするカスタムディレクティブを考えてみましょう。引数の値が縦方向のピクセル位置を更新するカスタムディレクティブを以下のように作成することができます:
7676

7777
```vue-html
7878
<div id="dynamic-arguments-example" class="demo">
@@ -87,15 +87,15 @@ const app = Vue.createApp({})
8787
app.directive('pin', {
8888
mounted(el, binding) {
8989
el.style.position = 'fixed'
90-
// binding.value is the value we pass to directive - in this case, it's 200
90+
// binding.value はディレクティブに渡した値です - このケースの場合、200 です
9191
el.style.top = binding.value + 'px'
9292
}
9393
})
9494

9595
app.mount('#dynamic-arguments-example')
9696
```
9797

98-
This would pin the element 200px from the top of the page. But what happens if we run into a scenario when we need to pin the element from the left, instead of the top? Here's where a dynamic argument that can be updated per component instance comes in very handy:
98+
これにより、ページの上端から 200px の位置に要素を固定できます。しかし、上端からではなく左端から要素をピン留めする必要があるシナリオが出てきたらどうでしょうか?ここでコンポーネントインスタンスごとに更新できる動的引数がとても便利です:
9999

100100
```vue-html
101101
<div id="dynamicexample">
@@ -116,7 +116,7 @@ const app = Vue.createApp({
116116
app.directive('pin', {
117117
mounted(el, binding) {
118118
el.style.position = 'fixed'
119-
// binding.arg is an argument we pass to directive
119+
// binding.arg がディレクティブに渡した引数です
120120
const s = binding.arg || 'top'
121121
el.style[s] = binding.value + 'px'
122122
}
@@ -125,7 +125,7 @@ app.directive('pin', {
125125
app.mount('#dynamic-arguments-example')
126126
```
127127

128-
Result:
128+
結果:
129129

130130
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="result" data-user="Vue" data-slug-hash="YzXgGmv" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Custom directives: dynamic arguments">
131131
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/YzXgGmv">
@@ -134,7 +134,7 @@ Result:
134134
</p>
135135
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
136136

137-
Our custom directive is now flexible enough to support a few different use cases. To make it even more dynamic, we can also allow to modify a bound value. Let's create an additional property `pinPadding` and bind it to the `<input type="range">`
137+
このカスタムディレクティブは、いくつかの違うユースケースをサポートできるほど柔軟になりました。さらに動的にするには、束縛した値を修正できるようにすれば良いでしょう。`pinPadding` という追加のプロパティを作成して、`<input type="range">` に束縛してみましょう。
138138

139139
```vue-html{4}
140140
<div id="dynamicexample">
@@ -155,7 +155,7 @@ const app = Vue.createApp({
155155
})
156156
```
157157

158-
Now let's extend our directive logic to recalculate the distance to pin on component update:
158+
さらにコンポーネントの更新に従ってピンとの距離を再計算できるようにディレクティブのロジックを拡張しましょう:
159159

160160
```js{7-10}
161161
app.directive('pin', {
@@ -171,7 +171,7 @@ app.directive('pin', {
171171
})
172172
```
173173

174-
Result:
174+
結果:
175175

176176
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="result" data-user="Vue" data-slug-hash="rNOaZpj" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Custom directives: dynamic arguments + dynamic binding">
177177
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/rNOaZpj">
@@ -180,9 +180,9 @@ Result:
180180
</p>
181181
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
182182

183-
## Function Shorthand
183+
## 関数による省略記法
184184

185-
In previous example, you may want the same behavior on `mounted` and `updated`, but don't care about the other hooks. You can do it by passing the callback to directive:
185+
前回の例では、`mounted` `updated` に同じ振る舞いを欲しかったでしょう。しかし、その他のフック関数を気にしてはいけません。ディレクティブにコールバックを渡すことで実現できます:
186186

187187
```js
188188
app.directive('pin', (el, binding) => {
@@ -192,9 +192,9 @@ app.directive('pin', (el, binding) => {
192192
})
193193
```
194194

195-
## Object Literals
195+
## オブジェクトリテラル
196196

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

199199
```vue-html
200200
<div v-demo="{ color: 'white', text: 'hello!' }"></div>
@@ -207,42 +207,42 @@ app.directive('demo', (el, binding) => {
207207
})
208208
```
209209

210-
## Usage on Components
210+
## コンポーネントにおける使用法
211211

212-
In 3.0, with fragments support, components can potentially have more than one root nodes. This creates an issue when a custom directive is used on a component with multiple root nodes.
212+
3.0 ではフラグメントがサポートされているため、コンポーネントは潜在的に1つ以上のルートノードを持つことができます。これは複数のルートノードを持つ1つのコンポーネントにカスタムディレクティブが使用された時に、問題を引き起こします。
213213

214-
To explain the details of how custom directives will work on components in 3.0, we need to first understand how custom directives are compiled in 3.0. For a directive like this:
214+
3.0 のコンポーネント上でどのようにカスタムディレクティブが動作するかを詳細に説明するために、3.0 においてカスタムディレクティブがどのようにコンパイルされるのかをまずは理解する必要があります。以下のようなディレクティブは:
215215

216216
```vue-html
217217
<div v-demo="test"></div>
218218
```
219219

220-
Will roughly compile into this:
220+
おおよそ以下のようにコンパイルされます:
221221

222222
```js
223223
const vDemo = resolveDirective('demo')
224224

225225
return withDirectives(h('div'), [[vDemo, test]])
226226
```
227227

228-
Where `vDemo` will be the directive object written by the user, which contains hooks like `mounted` and `updated`.
228+
ここで `vDemo` はユーザによって記述されたディレクティブオブジェクトで、それは `mounted` `updated` のフック関数を含みます。
229229

230-
`withDirectives` returns a cloned VNode with the user hooks wrapped and injected as VNode lifecycle hooks (see [Render Function](render-function.html) for more details):
230+
`withDirectives` は複製した VNode を返します。複製された VNode VNode のライフサイクルフック (詳細は[描画関数](render-function.html)を参照) としてラップ、注入されたユーザのフック関数を持ちます:
231231

232232
```js
233233
{
234234
onVnodeMounted(vnode) {
235-
// call vDemo.mounted(...)
235+
// vDemo.mounted(...) を呼びます
236236
}
237237
}
238238
```
239239

240-
**As a result, custom directives are fully included as part of a VNode's data. When a custom directive is used on a component, these `onVnodeXXX` hooks are passed down to the component as extraneous props and end up in `this.$attrs`.**
240+
**結果として、VNode のデータの一部としてカスタムディレクティブは全て含まれます。カスタムディレクティブがコンポーネントで利用される場合、これらの `onVnodeXXX` フック関数は無関係な props としてコンポーネントに渡され、最終的に `this.$attrs` になります。**
241241

242-
This also means it's possible to directly hook into an element's lifecycle like this in the template, which can be handy when a custom directive is too involved:
242+
これは以下のようなテンプレートのように、要素のライフサイクルに直接フックできることを意味しています。これはカスタムディレクティブが複雑すぎる場合に便利です:
243243

244244
```vue-html
245245
<div @vnodeMounted="myHook" />
246246
```
247247

248-
This is consistent with the [attribute fallthrough behavior](component-attrs.html). So, the rule for custom directives on a component will be the same as other extraneous attributes: it is up to the child component to decide where and whether to apply it. When the child component uses `v-bind="$attrs"` on an inner element, it will apply any custom directives used on it as well.
248+
これは [属性のフォールスロー](component-attrs.html) と一貫性があります。つまり、コンポーネントにおけるカスタムディレクティブのルールは、その他の異質な属性と同じです: それをどこにまた適用するかどうかを決めるのは、子コンポーネント次第です。子コンポーネントが内部の要素に `v-bind="$attrs"` を利用している場合、あらゆるカスタムディレクティブもその要素に適用されます。

0 commit comments

Comments
 (0)