【问题标题】:Vue.js "emit event" don't bubble up to parent and grandparent elementVue.js“发射事件”不会冒泡到父元素和祖父元素
【发布时间】:2019-12-28 10:47:52
【问题描述】:

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

<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】:

    你可以试试这样的:

    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>

    【讨论】:

    • 所以事件不会冒泡到根元素?您必须将事件交给父元素,然后从那里转发给父元素,直到您将它放在您想要的位置?
    • 是的,事件只冒泡到父级。
    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2018-05-29
    • 2018-05-19
    • 2013-10-22
    • 2020-08-07
    相关资源
    最近更新 更多