我有一個在父組件中初始化的方法,名爲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