Vue.js“发射事件”不会冒充父元素和祖父母元素

问题描述 投票:0回答:1

我想发出一个自定义事件,使之从孙子通过子元素冒到父元素。但它不起作用。即使直接在父元素上触发事件,子元素也不起作用。

<div id="example-app" >
   <parent>
      <child>
         <grandchild></grandchild>
      </child>
   </parent>
</div>

这里是组件代码:

Vue.component('parent', {
    template: ` <div @testevent="test" style="padding: 10px; background-color: red; border: 1px solid black;">
                    <button @click="$emit('testevent')">Parent Button</button>
                    <button @click="test">Triggger Test Manueally</button>

                    <slot ></slot>
                </div>`,
    methods: {
        test () {
            alert('Test ok')
        }
    }
})

Vue.component('child', {
    template: ` <div style="padding: 10px; margin: 5px; background-color: green; border: 1px solid black;">
                    <button @click="$emit('testevent')">Child Button</button><br>
                    <slot></slot>
                </div>`
})

Vue.component('grandchild', {
    template: `<div style="padding:10px; margin: 5px; background-color: white; border: 1px solid black;">
                <button @click="$emit('testevent')">Grandchild Button</button>
                </div>`
})

new Vue({
    el: '#example-app',
})
javascript vue.js events event-handling event-bubbling
1个回答
0
投票

您可以尝试这样的事情:

Vue.component('parent', {
    template: ` <div style="padding: 10px; background-color: red; border: 1px solid black;">
                    <button @click="$emit('testevent')">Parent Button</button>
                    <button @click="test">Triggger Test Manueally</button>

                    <slot ></slot>
                </div>`,
    methods: {
        test () {
            alert('Test ok')
        }
    },
    mounted: function() {
      this.$on('testevent', () => {
        this.test()
      })
    }
})

Vue.component('child', {
    template: ` <div style="padding: 10px; margin: 5px; background-color: green; border: 1px solid black;">
                    <button @click="$emit('testevent')">Child Button</button><br>
                    <slot></slot>
                </div>`,
    mounted: function() {
      this.$on('testevent', () => {
        this.$parent.$emit('testevent')
      })
    }
})

Vue.component('grandchild', {
    template: `<div style="padding:10px; margin: 5px; background-color: white; border: 1px solid black;">
                <button @click="$parent.$emit('testevent')">Grandchild Button</button>
                </div>`
})

new Vue({
    el: '#example-app',
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="example-app" >
   <parent>
      <child>
         <grandchild></grandchild>
      </child>
   </parent>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.