admin管理员组文章数量:1415460
I am having difficuly working out why the following code code is not working for a child and parent single page ponents.
Child.vue
<script>
export default {
name: 'ChildComponent',
data () {
return {}
}
mounted: function() {
this.emitSignal()
},
methods: {
emitSignal: function () {
console.log('>>emitSignal()')
this.$emit('my_signal')
}
}
}
</script>
Parent.vue
<template>
<div>
<child-ponent v-on:my_signal="doSomething"></child-ponent>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
name: 'ParentComponent',
data () {
return {
}
},
ponents: {
ChildComponent
},
methods: {
doSomething: function () {
console.log('doSomething')
}
}
}
</script>
If I run the application, I see the child emits the signal >>emitSignal() in console, but the parent function 'doSomething' does not run.
Any idea what I could be missing?
NB I also tried an event bus as per here: /, the child fires but again the parent does not intercep the signal and run the specified function.
I am having difficuly working out why the following code code is not working for a child and parent single page ponents.
Child.vue
<script>
export default {
name: 'ChildComponent',
data () {
return {}
}
mounted: function() {
this.emitSignal()
},
methods: {
emitSignal: function () {
console.log('>>emitSignal()')
this.$emit('my_signal')
}
}
}
</script>
Parent.vue
<template>
<div>
<child-ponent v-on:my_signal="doSomething"></child-ponent>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
name: 'ParentComponent',
data () {
return {
}
},
ponents: {
ChildComponent
},
methods: {
doSomething: function () {
console.log('doSomething')
}
}
}
</script>
If I run the application, I see the child emits the signal >>emitSignal() in console, but the parent function 'doSomething' does not run.
Any idea what I could be missing?
NB I also tried an event bus as per here: https://alligator.io/vuejs/global-event-bus/, the child fires but again the parent does not intercep the signal and run the specified function.
Share Improve this question asked Apr 25, 2017 at 7:25 proximacentauriproximacentauri 1,8796 gold badges29 silver badges55 bronze badges 1- 1 Use this.$parent.$emit('my_signal') instead. – Potray Commented Apr 25, 2017 at 7:28
1 Answer
Reset to default 2Your emit
code is fine, besides a ma missing after data
block in Child.vue
Working example: https://jsfiddle/63t082p2/42/
<div id="app">
<child v-on:my_signal="doSomething"></child>
</div>
new Vue({
el: '#app',
methods: {
doSomething: function () {
console.log('doSomething')
}
},
ponents: {
'child' : {
mounted: function() {
this.emitSignal()
},
methods: {
emitSignal: function () {
console.log('>>emitSignal()')
this.$emit('my_signal')
}
}
}
}
});
本文标签: javascriptVuejs emit not being received by parentStack Overflow
版权声明:本文标题:javascript - Vue.js $emit not being received by parent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745169075a2645861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论