Skip to content

Commit 58f35c9

Browse files
LinusBorgThorsten Luenborg
and
Thorsten Luenborg
authored
feat(Migration): accessing instance in a custom directive (#609)
Co-authored-by: Thorsten Luenborg <[email protected]>
1 parent e8cf8da commit 58f35c9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/guide/migration/custom-directives.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ app.directive('highlight', {
8383

8484
Now that the custom directive lifecycle hooks mirror those of the components themselves, they become easier to reason about and remember!
8585

86+
### Edge Case: Accessing the component instance
87+
88+
It's generally recommended to keep directives independent of the component instance they are used in. Accessing the instance from within a custom directive is often a sign that the directive should rather be a component itself. However, there are situations where this actually makes sense.
89+
90+
In Vue 2, the component instance had to be accessed through the `vnode` argument:
91+
92+
```javascript
93+
bind(el, binding, vnode) {
94+
const vm = vnode.context
95+
}
96+
```
97+
98+
In Vue 3, the instance is now part of the `binding`:
99+
100+
```javascript
101+
mounted(el, binding, vnode) {
102+
const vm = binding.instance
103+
}
104+
```
105+
86106
## Implementation Details
87107

88108
In Vue 3, we're now supporting fragments, which allow us to return more than one DOM node per component. You can imagine how handy that is for something like a component with multiple `<li>` elements or the children elements of a table:

0 commit comments

Comments
 (0)