Skip to content

Commit dee5886

Browse files
NataliaTepluhinabencodezen
authored andcommitted
fix: fixed custom directives on components
1 parent 000b53c commit dee5886

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/guide/custom-directive.md

+11-29
Original file line numberDiff line numberDiff line change
@@ -209,40 +209,22 @@ app.directive('demo', (el, binding) => {
209209

210210
## Usage on Components
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.
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).
215213

216214
```vue-html
217-
<div v-demo="test"></div>
218-
```
219-
220-
Will roughly compile into this:
221-
222-
```js
223-
const vDemo = resolveDirective('demo')
224-
225-
return withDirectives(h('div'), [[vDemo, test]])
215+
<my-component v-demo="test"></my-component>
226216
```
227217

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-
232218
```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+
})
238226
```
239227

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"`.
247229

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

Comments
 (0)