I’m searching to trigger event from a vue instance which is encapsulated inside a shadowDOM.
For the moment, my code create a new Vue instance inside a shadowDOM and print the template without the style (because nuxt load the style inside the base vue instance).
I can call methods of each instance
But, when i’m trying to add an event / listener on my component, it doesn’t work.
DOM.vue
JavaScript
x
49
49
1
<template>
2
<section ref='shadow'></section>
3
</template>
4
5
<script>
6
import bloc from '@/components/bloc.vue'
7
import Vue from 'vue'
8
9
export default {
10
data() {
11
return {
12
vm: {},
13
shadowRoot: {},
14
isShadowReady: false
15
}
16
},
17
watch: {
18
isShadowReady: function() {
19
let self = this
20
this.vm = new Vue({
21
el: self.shadowRoot,
22
components: { bloc },
23
template: "<bloc @click='listenClick'/>",
24
methods: {
25
listenClick() { console.log("I clicked !") },
26
callChild() { console.log("I'm the child !") }
27
},
28
created() {
29
this.callChild()
30
self.callParent()
31
}
32
})
33
}
34
},
35
mounted() {
36
const shadowDOM = this.$refs.shadow.attachShadow({ mode: 'open' })
37
this.shadowRoot = document.createElement('main')
38
shadowDOM.appendChild(this.shadowRoot)
39
this.isShadowReady = true
40
},
41
methods: {
42
callParent() {
43
console.log("I'am the parent")
44
},
45
}
46
}
47
</script>
48
49
bloc.vue
JavaScript
1
19
19
1
<template>
2
<div>
3
<p v-for='word in words' style='text-align: center; background: green;'>
4
{{ word }}
5
</p>
6
</div>
7
</template>
8
9
10
<script>
11
export default {
12
data() {
13
return {
14
words: [1,2,3,4,5,6]
15
}
16
}
17
}
18
</script>
19
Any idea ?
Thank you
Advertisement
Answer
bloc @click.native=’listenClick’/>