私は子供component
を持っていて、そのデータを親に渡したいと思っています。 私の子コンポーネントは次のようになります。私はemitのハンドラを置くべきですか?
// <button @click="sendClick($event)">Send</button>
// ...
data: function(){
return {
mycode: ""
}
},
methods: {
sendClick(e)
{
bus.$emit('change', this.mycode);
}
}
私の親コンポーネントが見えます:
var app = new Vue({
el: '#app',
data: {
currentView: 'past-form',
mycode: ''
},
methods:
{
changeView()
{
this.currentView = 'past-form'
console.log(this.mycode);
},
},
created()
{
bus.$on('change', function(mycode){
this.mycode = mycode;
});
}
})
私は(
bus
がグローバルに宣言された)bus.$on
を配置するためのより良い場所を発見していないcreated()
よりも、ドキュメントは、created()
は、ページがロードされた後に初期化されるべきものであると述べています。created()
ブロックが動作します。console.log(this.mycode)
に入れてチェックしましたが、別の場所でハンドラを動かすべきですか?console.log(this.mycode);
は何も印刷しないので、私のコードはmycode: ''
を実行しないようです。
あなたの子供はヴューの直接の子である場合は、バスのための必要はありません。 – Bert