>我有两个组件:父级和子级。
在子组件中,我创建用户事件:
someMethod() {
this.$emit('init-event', 100)
}
父组件:
getChildEventValue() {
// how now get value from event 'init-event' without v-on directive in template?
}
典型和简单的方法是简单地使用事件总线。有几种不同的方法可以创建全局事件总线。 在我的设置中,我按如下方式进行操作
在主要的js...
Vue.prototype.$Bus = new Vue();
然后在您的子组件中,或兄弟姐妹或任何地方......
this.$Bus.$emit('myEvent');
然后在其他组件中侦听事件。 也许在挂钩山上...
mounted(){
this.$Bus.$on('myEvent', function(){
console.log('receive the event!')
} );
}