似乎 Vue.js 2.0 不会从孙子组件向其祖父组件发出事件。
Vue.component('parent', {
template: '<div>I am the parent - {{ action }} <child @eventtriggered="performAction"></child></div>',
data(){
return {
action: 'No action'
}
},
methods: {
performAction() { this.action = 'actionDone' }
}
})
Vue.component('child', {
template: '<div>I am the child <grand-child></grand-child></div>'
})
Vue.component('grand-child', {
template: '<div>I am the grand-child <button @click="doEvent">Do Event</button></div>',
methods: {
doEvent() { this.$emit('eventtriggered') }
}
})
new Vue({
el: '#app'
})
这个 JsFiddle 解决了这个问题https://jsfiddle.net/y5dvkqbd/4/,但是通过两个事件:
- 一个从孙子到中间组件
- 然后再次从中间组件发射到祖父
添加这个中间事件似乎是重复和不必要的。有没有办法直接发送给我不知道的祖父母?