1. 声明一个空的Vue模块eventBus
import Vue from 'vue'
/**
* 定义空的vue实例,作为 eventbus实现非父子组件之间的通信(vue2.x中去掉了broadcast)
*/
var eventBus = new Vue({});
export default eventBus;
2. 通过eventBus.$emit传参给上一个页面
import eventBus from '../service/eventbus.js';
methods:{
//列表选中具体医院的点击事件
goback(hospital){
//传递一个map,choiceHospital是key,hospital是value
eventBus.$emit('choiceHospital',hospital);
//调用router回退页面
this.$router.go(-1);
}
}
3. 页面接收参数
import eventBus from '../service/eventbus.js';
//每次激活时
activated(){
//根据key名获取传递回来的参数,data就是map
eventBus.$off('choiceHospital') // -------------------------- 一定要先关闭监听,不然会打印多次
eventBus.$on('choiceHospital', function(data){
//赋值给首页的附近医院数据模型
this.nearestOrg = data;
}.bind(this));
},
网友评论