我想要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',
})
答案 0 :(得分: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>