事件总线bus传参

8 篇文章 1 订阅

 Bus作为一个中转站,实现非父子之间组件之间的通信.

  bus本质上是vue对象,传递的方法本质上就是emit与on

创建文件

3ce9b9e330b240279893341a60c936a6.png

 页面引入

92eae11923544741b7924aa621b9e41d.png

watch监听tab切换后进行bus.$emit传参,在另一个侧边栏组件是使用了Bus.$on获取数据的,但没成功刷新视图

  watch: {
    
    $route: {
      handler: function(route) {
        if (route.path.includes("/StatisticThing")) {
          this.isActive = "2";
        } else if (route.path.includes("/officePlatform/")) {
          Bus.$emit("ulLi2", this.officePlatformList);

          this.isActive = "1";
        } else if (route.path.includes("/examineGrade")) {
          this.isActive = "4";
        } else if (route.path.includes("/officeRiskInvestigation")) {
            Bus.$emit("ulLi2", this.riskInvestigationList);

          this.isActive = "7";
        }
      },
      immediate: true
    }
  },

上方代码的问题在哪呢,我们需要延迟传参事件,使用setTimeout就可以解决

  watch: {
    
    $route: {
      handler: function(route) {
        if (route.path.includes("/StatisticThing")) {
          this.isActive = "2";
        } else if (route.path.includes("/officePlatform/")) {
 setTimeout(() => {

          Bus.$emit("ulLi2", this.officePlatformList);
          }, 100);

          this.isActive = "1";
        } else if (route.path.includes("/examineGrade")) {
          this.isActive = "4";
        } else if (route.path.includes("/officeRiskInvestigation")) {
          setTimeout(() => {

            Bus.$emit("ulLi2", this.riskInvestigationList);
          }, 100);

          this.isActive = "7";
        }
      },
      immediate: true
    }
  },

一波未平一波又起,突然发现tab切换后选中状态丢了,接参数的时候来一个延迟就又行了,控制台打印发现不这样整mounted里的事件比created都要先执行

created() {
   this.getList();
  },
  mounted() {
    setTimeout(() => {
      this.checked();
    }, 100);

  },
  methods: {
    getList() {
      console.log("已执行 :>> ", "已执行");
      Bus.$on("ulLi2", ulLi2 => {
        this.ulLi = ulLi2;
        console.log("this.ulLi,ulLi2 :>> ", this.ulLi, ulLi2);
      });
    },
    checked() {
      var path = window.location.href;
      var i = path.indexOf("#"),
        i2 = path.indexOf("?"),
        pathTest = "";
      if (i > -1 && i2 > -1) {
        pathTest = path.slice(i + 1, i2);
      } else if (i > -1 && i2 == -1) {
        pathTest = path.slice(i + 1, path.length);
      }
      var checked = this.steamroller(this.ulLi);
      console.log("checked :>> ", checked);
      checked.forEach(ele => {
        if (ele.url) {
          var path = ele.url.split("?")[0];
          if (path == pathTest) {
            this.isActive = ele.id;
          }
        }

        //     if(ele.url == pathTest){
        //         this.isActive= ele.id
        //    }
      });
    },

事实上bus传参是真不好用,实现方式都是跟vuex似的创建全局变量或方法,但bus传参emit时经常因为执行顺序问题导致on接收不到,兄弟组件之间的渲染顺序本就大差不差,当需要emit传递的数据稍微多一点,on就接收不到了,这个时候只能延时emit发送

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JianZhen✓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值