【发布时间】:2018-02-10 21:28:03
【问题描述】:
我有两个 Vue 组件。 parent-component:
Vue.component('parent-component',{
methods: {
test: function(){
alert('Option Selected');
}
},
template: `
<div><slot></slot></div>
`
});
还有animals 组件:
Vue.component('animals',{
data: function(){
return {
selected: ''
}
},
template: `
<select @change="selectionChanged" v-model="selected">
<slot></slot>
</select>
`,
methods: {
selectionChanged: function(){
this.$emit('optionselected', this.selected);
}
}
});
这是我的 HTML:
<div id="app">
<parent-component @optionselected="test()">
<animals>
<option>Aardvark</option>
<option>Bear</option>
<option>Cat</option>
</animals>
</parent-component>
</div>
我正在尝试将所选选项从子组件 (animals) 获取到父组件 (parent-component)。我正在从子级发出 optionselected 事件,但看起来父组件没有响应该事件,我的意思是根本没有调用 test() 方法。我在这里做错了什么?
【问题讨论】:
标签: javascript vue.js vue-component