It seems that Vue.js 2.0 doesn’t emit events from a grand child to his grand parent component.
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' })
This JsFiddle solves the issue https://jsfiddle.net/y5dvkqbd/4/ , but by emtting two events:
- One from grand child to middle component
- Then emitting again from middle component to grand parent
Adding this middle event seems repetitive and unneccessary. Is there a way to emit directly to grand parent that I am not aware of?
Advertisement
Answer
NEW ANSWER (Nov-2018 update)
I discovered that we could actually do this by leveraging the $parent
property in the grand child component:
this.$parent.$emit("submit", {somekey: somevalue})
Much cleaner and simpler.