全局时间总线,兄弟间传递信息

在 入口文件 main中引入bus



new Vue({
  render: h => h(App),
  //注册路由
  router,
  //注册store
  store,
  //配置全局总线
  beforeCreate() {
    Vue.prototype.$bus = this
  },
}).$mount('#app')

在需要调用传递的函数中 声明要调用的 bus名称

methods:{
    removeKeyWord(){
      this.searchParams.keyword = undefined;
      this.goData();
      this.$bus.$emit("clear")
    }
}

在接受信息的地方配置调用函数

  mounted() {
    this.$bus.$on("clear",()=>{
        this.keyword=''
      }
    )
  },

posted on 2022-08-28 17:54  xiaobo95  阅读(19)  评论(0)    收藏  举报

导航