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
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.
213
-
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:
212
+
When used on components, custom directive will always apply to component's root node, similarly to [non-prop attributes](component-attrs.html).
215
213
216
214
```vue-html
217
-
<div v-demo="test"></div>
218
-
```
219
-
220
-
Will roughly compile into this:
221
-
222
-
```js
223
-
constvDemo=resolveDirective('demo')
224
-
225
-
returnwithDirectives(h('div'), [[vDemo, test]])
215
+
<my-component v-demo="test"></my-component>
226
216
```
227
217
228
-
Where `vDemo` will be the directive object written by the user, which contains hooks like `mounted` and `updated`.
229
-
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):
231
-
232
218
```js
233
-
{
234
-
onVnodeMounted(vnode) {
235
-
// call vDemo.mounted(...)
236
-
}
237
-
}
219
+
app.component('my-component', {
220
+
template:`
221
+
<div> // v-demo directive will be applied here
222
+
<span>My component content</span>
223
+
</div>
224
+
`
225
+
})
238
226
```
239
227
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`.**
241
-
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:
243
-
244
-
```vue-html
245
-
<div @vnodeMounted="myHook" />
246
-
```
228
+
Unlike attributes, directives can't be passed to a different element with `v-bind="$attrs"`.
247
229
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.
230
+
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