实现各个组件之间(父子,兄弟)的通信可以使用vuex和bus事件总线,使用bus总线实现通信的方法如下:
参考资料:
https://www.cnblogs.com/shj-com/p/10337782.html
https://www.cnblogs.com/wenxuehai/p/10509839.html
我采取了将Bus抽离出来,
import Vue from 'vue'
const bus= new Vue()
export default Bus
然后在各个组件中引入的方法,但是在使用过程中发现一个问题,信息接收端能够接收到传递的信息,但是无法被信息接收端使用,接收端的代码如下:
bus.$on('choosedImgUrl',function(url){
this.choosedImgUrl = url;
});
通过百度发现了问题的所在:
http://www.imooc.com/wenda/detail/440363
因此,修改代码,将this改成bus,然后再次赋值给信息接收端的data便可以了:
bus.$on('choosedImgUrl',function(url){
bus.choosedImgUrl = url;
});
this.choosedImgUrl = bus.choosedImgUrl;