我有一个在父组件中初始化的方法,名为setMessage()
,我希望能够在子组件中调用它。在vue中使用父函数的子组件js
main.js
const messageBoard = new Vue({
el: '#message-board',
render: h => h(App),
})
应用(App.vue(父))..
export default {
data() {
return { messages: state }
},
methods: {
setMessage(message) {
console.log(message);
}
},
template: `
<div>
<child-component></child-component>
</div>
`,
}
孩子
const child = Vue.extend({
mounted() {
// attempting to use this function from the parent
this.$dispatch('setMessage', 'HEY THIS IS MY MESSAGE!');
}
});
Vue.component('child-component', child);
现在我得到this.$dispatch is not a function
错误消息。我究竟做错了什么?我怎样才能在各种子组件中使用父功能?我也试过$emit
,它不会抛出错误&它不会触及该函数。
非常感谢您的帮助!
您使用的是哪个版本的Vue? – Peter
@Peter'2.1.10' – Modelesq