使用privide/inject和this.$refs/this.$root进行通信

介绍

通常在vue中我们会经常使用到一些组件间的通信方法,比如父-子、子-父、兄弟、vue-bus、vuex等,那么除了这些还有没有其他的通信方式呢。当然,今儿我就找到了另外的两种方法,privide/inject和this.$ refs / this.$ root
注意以下步骤所用代码都来自完整代码,为了方便阅读,有些地方进行了删除

privide/inject及this.$ refs/this.$ root

完整代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <father> </father>
    </div>
    <script>
      Vue.component("father", {
        provide: {
          foo: "father的值",
        },

        data() {
          return {
            msg: "老父亲的值",
          };
        },
        mounted() {
          console.log(this.foo);

          console.log("在父亲中拿全局", this.$root.msg);
        },
        template: `
          <div>

              <children>

               </children>
           </div>
        `,
      });
      Vue.component("children", {
        inject: ["foo"],
        data() {
          return {
            msg: "儿子的值",
          };
        },
        template: `<div>
          <!-- 给孙子的组件绑定一个ref -->
          {{this.foo}}
          <grandSon ref='grandSonData'></grandSon>
                  </div>`,
        mounted() {
          // 可以通过this.$refs.grandSonData.msg拿到值
          console.log(this.$refs.grandSonData.msg);
          console.log("在儿子中拿全局", this.$root.msg);
        },
      });
      Vue.component("grandSon", {
        inject: ["foo"],

        data() {
          return {
            msg: "孙子的值",
          };
        },
        mounted() {
          console.log(this.foo);
          console.log("在孙子中拿全局", this.$root.msg);
        },
        template: `<div></div>`,
      });
      new Vue({
        el: "#app",
        data: {
          msg: "全局的值",
        },
        mounted() {
          console.log("在全局中拿全局", this.msg);
        },
      });
    </script>
  </body>
</html>

以上代码就是两种方式的介绍

1.privide/inject

这种方法俗称跨代传值,虽说夸代但是从父-子也是可以进行传递的,通过再父组件中通过provide发放要传递的值,如下:

Vue.component("father", {
        provide: {
          foo: "father的值",
        },

        data() {
          return {
            msg: "老父亲的值",
          };
        },
       
        template: `
          <div>

              <children>

               </children>
           </div>
        `,
      });
 

在子组件中通过inject来接收值,无论是其儿子还是孙子都可以接收到父组件中的foo传递过来的值,如下:

 Vue.component("children", {
        inject: ["foo"],
        data() {
          return {
            msg: "儿子的值",
          };
        },
        template: `<div>
          <!-- 给孙子的组件绑定一个ref -->
          {{this.foo}}
          <grandSon ref='grandSonData'></grandSon>
                  </div>`,
      
      });
   Vue.component("grandSon", {
        inject: ["foo"],

        data() {
          return {
            msg: "孙子的值",
          };
        },
       
        template: `<div></div>`,
      });

2.this.$ refs和this.$ root

this.$ refs

这种方法是在父组件中使用子组件在子组件中绑定一个名为ref的attribute来访问子组件对象,如下:
在children组件中使用grandson组件,并且在grandson组件绑定一个属性ref并命名为grandSonData

 Vue.component("children", {
      
        data() {
          return {
            msg: "儿子的值",
          };
        },
        template: `<div>
          <!-- 给孙子的组件绑定一个ref -->
          
          <grandSon ref='grandSonData'></grandSon>
                  </div>`,
        mounted() {
          // 可以通过this.$refs.grandSonData.msg拿到值
          console.log(this.$refs.grandSonData.msg);
          console.log("在儿子中拿全局", this.$root.msg);
        },
      });

此时我们就可以在dom节点加载完毕后的钩子函数中通过this.$refs.grandSonData.msg拿到grandson的msg数据

this.$ root

在子组件中我们可以通过this.$ root拿到全局的对象的数据

     Vue.component("father", {
        

       
        mounted() {
          
          console.log("在父亲中拿全局", this.$root.msg);
        },
        template: `
          <div>

              <children>

               </children>
           </div>
        `,
      });
      Vue.component("children", {
       
        data() {
          return {
            msg: "儿子的值",
          };
        },
        template: `<div>
          <!-- 给孙子的组件绑定一个ref -->
         
          <grandSon></grandSon>
                  </div>`,
        mounted() {
         
          console.log("在儿子中拿全局", this.$root.msg);
        },
      });
      Vue.component("grandSon", {
        

        data() {
          return {
            msg: "孙子的值",
          };
        },
        mounted() {
         
          console.log("在孙子中拿全局", this.$root.msg);
        },
        template: `<div></div>`,
      });
       new Vue({
        el: "#app",
        data: {
          msg: "全局的值",
        },
        mounted() {
          console.log("在全局中拿全局", this.msg);
        },
      });

请观察三个子组件,father、children、grandson,这三个子组件的mounted钩子函数中都输出了this.$ root.msg,而这个msg就是全局中的msg也就是new Vue下面data里的msg,所以可以再子组件中通通过this.$ root.msg访问全局中的data数据。

以上就是其余两种通信方式,不足之处请大家指出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值