任意组件传值 bus传值
util下面创建bus.js文件
import Vue from "vue";
export default Vue.prototype.bus = new Vue();
main.js 引入
import bus from "@/util/bus";
挂载
Vue.prototype.bus = bus;
组件使用
组件A调用bus,定义方法名threeTowmsLayer,后面接的是需要传递的参数
this.bus.$emit("threeTowmsLayer", this.mapConfig.wmsLayer);
组件B在created里面先定义bus的方法值接收
同时记得要在销毁前销毁方法避免重复调用
注意在组件A调用bus时 要确认组件B已经存在渲染哦
created () {
this.bus.$on("threeTowmsLayer", (data) => {
this.threeCheckguanwangList = data;
});
},
beforeDestroy () {
this.bus.$off("threeTowmsLayer");
},