【发布时间】:2017-06-21 02:44:33
【问题描述】:
我的 vue 应用程序正在使用:
component-child 组成的component-parent组件
在父组件内部我有按钮,当有人单击按钮时我想发出一个事件以便由 vue 处理并传递给另一个组件
到目前为止我做了什么:
var vm = new Vue({
el: '#app',
methods: {
itemSelectedListener: function(item){
console.log('itemSelectedListener', item);
}
}
});
Vue.component('component-child', {
template: ' <span v-on:click="chooseItem(pty )" >Button </span>',
methods: {
chooseItem: function(pty){
console.log(pty);
this.$emit('itemSelected', {
'priority' : pty
});
}
}
});
Vue.component('component-parent', {
template: '<component-child v-for="q in items" ></component-child>'
});
HTML:
<component-parent v-on:itemSelected="itemSelectedListener" ></component-parent>
它到达了我的console.log(pty); 行,但似乎this.$emit('itemSelected' 无法通过:
console.log('itemSelectedListener', item); // this is not going to be called...
提示?
我应该从 child->parent->Vue-instance 冒泡事件吗? (我也试过了,但没有成功)
【问题讨论】:
标签: javascript vuejs2