Skip to content

Commit fd605a8

Browse files
committed
1 parent 4e3090b commit fd605a8

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/guide/custom-directive.md

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,40 +194,22 @@ app.directive('demo', (el, binding) => {
194194

195195
## コンポーネントにおける使用法
196196

197-
3.0 ではフラグメントがサポートされているため、コンポーネントは潜在的に1つ以上のルートノードを持つことができます。これは複数のルートノードを持つ1つのコンポーネントにカスタムディレクティブが使用された時に、問題を引き起こします。
198-
199-
3.0 のコンポーネント上でどのようにカスタムディレクティブが動作するかを詳細に説明するために、3.0 においてカスタムディレクティブがどのようにコンパイルされるのかをまずは理解する必要があります。以下のようなディレクティブは:
197+
When used on components, custom directive will always apply to component's root node, similarly to [non-prop attributes](component-attrs.html).
200198

201199
```vue-html
202-
<div v-demo="test"></div>
203-
```
204-
205-
おおよそ以下のようにコンパイルされます:
206-
207-
```js
208-
const vDemo = resolveDirective('demo')
209-
210-
return withDirectives(h('div'), [[vDemo, test]])
200+
<my-component v-demo="test"></my-component>
211201
```
212202

213-
ここで `vDemo` はユーザによって記述されたディレクティブオブジェクトで、それは `mounted``updated` のフック関数を含みます。
214-
215-
`withDirectives` は複製した VNode を返します。複製された VNode は VNode のライフサイクルフック (詳細は[Render 関数](render-function.html)を参照) としてラップ、注入されたユーザのフック関数を持ちます:
216-
217203
```js
218-
{
219-
onVnodeMounted(vnode) {
220-
// vDemo.mounted(...) を呼びます
221-
}
222-
}
204+
app.component('my-component', {
205+
template: `
206+
<div> // v-demo directive will be applied here
207+
<span>My component content</span>
208+
</div>
209+
`
210+
})
223211
```
224212

225-
**結果として、VNode のデータの一部としてカスタムディレクティブは全て含まれます。カスタムディレクティブがコンポーネントで利用される場合、これらの `onVnodeXXX` フック関数は無関係な props としてコンポーネントに渡され、最終的に `this.$attrs` になります。**
226-
227-
これは以下のようなテンプレートのように、要素のライフサイクルに直接フックできることを意味しています。これはカスタムディレクティブが複雑すぎる場合に便利です:
228-
229-
```vue-html
230-
<div @vnodeMounted="myHook" />
231-
```
213+
Unlike attributes, directives can't be passed to a different element with `v-bind="$attrs"`.
232214

233-
これは [属性のフォールスロー](component-attrs.html) と一貫性があります。つまり、コンポーネントにおけるカスタムディレクティブのルールは、その他の異質な属性と同じです: それをどこにまた適用するかどうかを決めるのは、子コンポーネント次第です。子コンポーネントが内部の要素に `v-bind="$attrs"` を利用している場合、あらゆるカスタムディレクティブもその要素に適用されます。
215+
With [fragments](/guide/migration/fragments.html#overview) support, components can potentially have more than one root nodes. When applied to a multi-root component, directive will be ignored and the warning will be thrown.

0 commit comments

Comments
 (0)