Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Active events in vue instance encapsulated inside shadowDOM

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

    <template>
        <section ref='shadow'></section>
    </template>
    
    <script>
        import bloc from '@/components/bloc.vue'
        import Vue from 'vue'
    
        export default {
            data() {
                return {
                    vm: {},
                    shadowRoot: {},
                    isShadowReady: false
                }
            },
            watch: {
                isShadowReady: function() {
                    let self = this
                    this.vm = new Vue({
                        el: self.shadowRoot,
                        components: { bloc },
                        template: "<bloc @click='listenClick'/>",
                        methods: {
                            listenClick() { console.log("I clicked !") },
                            callChild() { console.log("I'm the child !") }
                        },
                        created() {
                            this.callChild()
                            self.callParent()
                        }
                    })
                }
            },
            mounted() {
                const shadowDOM = this.$refs.shadow.attachShadow({ mode: 'open' })
                this.shadowRoot = document.createElement('main')
                shadowDOM.appendChild(this.shadowRoot)
                this.isShadowReady = true
            },
            methods: {
                callParent() {
                    console.log("I'am the parent")
                },
            }
        }
    </script>

bloc.vue

    <template>
        <div>
            <p v-for='word in words' style='text-align: center; background: green;'>
                {{ word }}
            </p>
        </div>
    </template>
    
    
    <script>
        export default {
            data() {
                return {
                    words: [1,2,3,4,5,6]
                }
            }
        }
    </script>

Any idea ?

Thank you

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Hi so after i watched at your code i think it's better to use the $emit to pass event from the child to the parent and i think it's more optimized then a @click.native

template: "<bloc @someevent='listenClick'/>",
<template>
  <div @click="listenClickChild">
    <p v-for='(word, idx) in words' :key="idx" style='text-align: center; background: green;'>
      {{ word }}
    </p>
  </div>
</template>


<script>
export default {
  data() {
    return {
      words: [1,2,3,4,5,6]
    }
  },
  methods: {
    listenClickChild() {
      this.$emit('someevent', 'someValue')
    }
  }
}
</script>
about 4 years ago · Juan Pablo Isaza Report

0

bloc @click.native='listenClick'/>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!