Skip to content

Commit d61ef3d

Browse files
committed
web-components.md - add event handling caveat
1 parent 3251536 commit d61ef3d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/guide/extras/web-components.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We consider Vue and Web Components to be primarily complementary technologies. V
66

77
## Using Custom Elements in Vue
88

9-
Vue [scores a perfect 100% in the Custom Elements Everywhere tests](https://custom-elements-everywhere.com/libraries/vue/results/results.html). Consuming custom elements inside a Vue application largely works the same as using native HTML elements, with a few things to keep in mind:
9+
Consuming custom elements inside a Vue application largely works the same as using native HTML elements, with a few things to keep in mind:
1010

1111
### Skipping Component Resolution
1212

@@ -75,6 +75,30 @@ However, there could be rare cases where the data must be passed as a DOM proper
7575
<my-element .user="{ name: 'jack' }"></my-element>
7676
```
7777

78+
### Handling Events with Capital Letters
79+
80+
The `v-on` directive handles lowercase and kebab-case events dispatched from custom elements. However, the `v-on` directive does not support listening for events whose names include capital letters. For example, adding `v-on:myInput` to a custom element adds a listener for events with the name `my-input`, which does not handle an event with the name `myInput`.
81+
82+
If your app uses custom elements that dispatch events with capital letters, you can use a custom directive to add the correct event listener to the element.
83+
84+
#### Example Custom Directive to Handle Specific Events
85+
86+
```js
87+
// ex. <my-element v-event:eventName="handler" />
88+
app.directive('event', {
89+
beforeMount(el, { arg: eventName, value: handler }) {
90+
el.addEventListener(eventName, handler)
91+
},
92+
beforeUnmount(el, { arg: eventName, value: handler }) {
93+
el.removeEventListener(eventName, handler)
94+
}
95+
})
96+
```
97+
98+
**See also:**
99+
- [Custom Directives](/guide/reusability/custom-directives.html)
100+
- [Built-in "v-on" Directive](/api/built-in-directives.html#v-on)
101+
78102
## Building Custom Elements with Vue
79103

80104
The primary benefit of custom elements is that they can be used with any framework, or even without a framework. This makes them ideal for distributing components where the end consumer may not be using the same frontend stack, or when you want to insulate the end application from the implementation details of the components it uses.

0 commit comments

Comments
 (0)