美文网首页
vue-router go(-1)后退时怎么带参数?

vue-router go(-1)后退时怎么带参数?

作者: 稀释1 | 来源:发表于2021-05-18 14:53 被阅读0次

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));
},

相关文章

网友评论

      本文标题:vue-router go(-1)后退时怎么带参数?

      本文链接:https://www.haomeiwen.com/subject/uttyjltx.html