0
我有分量,里面我做冒落:不能从一个孩子发出另一个
methods: {
sendClick(e)
{
bus.$emit('codechange', this.mycode);
console.log(this.selectedLanguage);
this.$parent.sendCode();
this.$parent.changeView();
}
}
在父组件我hadling数据:
var app = new Vue({
el: '#app',
data: {
currentView: 'past-form',
mycode: ''
},
methods:
{
changeView()
{
console.log(this.mycode);
},
sendCode()
{
console.log("Code is sended to server");
this.currentView = 'view-form';
bus.$emit('codechange', this.mycode);
}
},
created()
{
bus.$on('codechange', function(mycode){
console.log("test"); // this works
this.mycode = mycode; // this works
}.bind(this));
}
})
处理在父母工作精细。但点击sendCode()
我想发送数据到第三个组件。第三个组件代码:
Vue.component('view-form', {
template: `
<div class="ViewCodeContainer">
<div class="ViewCode">my code here</div>
<code> {{mycode}} </code>
<div class="ViewCodeMenu">my menu here</div>
</div>`,
data() {
return {
mycode: ''
}
},
created()
{
bus.$on('codechange', function(mycode){
console.log("hererere");
console.log(mycode);
this.mycode = mycode;
}.bind(this));
console.log("test");
}
})
但是,处理代码不起作用。块console.log("hererere");
未执行。我做错了什么?
看起来像“view-form”组件根本就没有创建。确保它出现在你的模板中。或者console.log在listener之前的'created'钩子中。否则它应该工作:https://jsfiddle.net/wostex/63t082p2/74/ – wostex
它的创建,因为我可以切换到它。如果我是硬编码'我的代码'它,我可以看到它 –
您是否正在使用'is'设置为'currentView'的动态组件? – Bert