親コンポーネント内でsetMessage()
というメソッドを初期化しました。子コンポーネント内で呼び出せるようにしたいと考えています。vue jsの親機能を使用する子コンポーネント
main.js
const messageBoard = new Vue({
el: '#message-board',
render: h => h(App),
})
の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