《vue- 兄弟组件之间的通信传值》

Posted 爱听书的程序猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《vue- 兄弟组件之间的通信传值》相关的知识,希望对你有一定的参考价值。

需求:右侧界面上一个新增菜单的弹框,点击确定按钮之后左侧的菜单也需要动态新增,这时就用到了兄弟组件之间的传值方式:

未添加菜单时左侧菜单为空

 

 

 

 点击确定之后:

 

 

 

 实现:

1.  添加菜单的弹框界面中form提交时

import bus from \'@/utils/bus\'
 
//form提交
    confirm(formName)
      this.$refs[formName].validate((valid) =>
        if (valid)
            this.loadingbut = true;
            this.loadingtext = "提交中";
            let formData = new FormData();
            formData.append(\'Code\', this.form.Code)
            request(
              url: \'/api/Product/Create\',
              method: \'post\',
              data: formData,
              headers:
                "Content-Type": "multipart/form-data","Authorization": "Bearer " + getToken()
             
            ).then(res =>
                bus.$emit(\'menueChange\', \'菜单需要刷新\');//向菜单组件传递信息
                this.$message(type: "success",message:res.message);
                this.exit();
                this.$emit("getProductListData");
            ).catch(error =>
              console.log("添加失败: ",error)
            ).finally(() =>
              this.loadingbut = false;
              this.loadingtext = "确定";
            );
         
      );
    ,

2.  菜单组件中接收

import bus from \'@/utils/bus\'
 
  mounted()
    bus.$on(\'menueChange\', (e) =>
      console.log(e);
      this.getMenuData(); //接收成功,执行自己的逻辑操作,一般都是先刷新菜单数据
    )
   
  ,
 

3.  bus.js

import Vue from \'vue\'
const bus = new Vue()
export default bus

 

以上是关于《vue- 兄弟组件之间的通信传值》的主要内容,如果未能解决你的问题,请参考以下文章

Vue 父子组件、兄弟组件传值

vue的$emit 与$on父子组件与兄弟组件的之间通信

兄弟组件怎么粘在一起css

vue父子组件之间的通信

VUE父子组件之间的传值,以及兄弟组件之间的传值

vue-兄弟组件传值