我有孩子component
并想将一些数据传递给它的父项。 我的子组件的样子:我应该在哪里放置处理程序?
// <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.$on
(bus
全局声明)比created()
,但是文档声明created()
适用于在页面加载后应该初始化的内容。created()
块的工作;我检查了它console.log(this.mycode)
,但我应该移动发射处理程序在其他地方?这看起来像我的代码不执行
mycode: ''
,因为console.log(this.mycode);
不会打印任何东西。
如果你的孩子是Vue公司的直接孩子,没有必要一辆公交车。 – Bert