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

news/2024/7/10 0:39:46 标签: vue

介绍

通常在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数据。

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


http://www.niftyadmin.cn/n/1206104.html

相关文章

js焦点轮播图

汇集网上焦点轮播图的实现方式&#xff0c;自己试了下&#xff0c;不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效&#xff0c;不太明白&#xff0c;最后两行代码中&#xff0c;为什么可以直接写stop和play。不用加括号调用函数么&#xff1f;求懂的大神指点&#xff01; 所…

RPA之家UiPath视频教程

【RPA之家】UiPath Studio下载安装和激活 https://www.bilibili.com/video/BV18J411H7K7 【RPA之家】UiPath变量的介绍和使用 https://www.bilibili.com/video/BV18J411H7gy 【RPA之家】UiPath第一个案例HelloWorld https://www.bilibili.com/video/BV18J411H7Vp 【RPA之家】Ui…

Vim常用的总结

地址定界遵循正则表达式。1、HJKL上下左右移动2、I插入3、ZZ保存退出4、w /PATH/TO/SOMEFIEL 保存在指定路径5、dd删除当前行6、yy复制当前行7、p粘贴编辑模式&#xff1a;地址定界&#xff1a;1&#xff0c;2表示1到2行/pattern/,/pattern/sg&表示地址定界找到的东西\(XX\…

翻译记忆软件-塔多思TRADO经典教程_2

Trados 中文简明教程Trados 中文简明教程1. 准备工作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 准备MultiTerm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . …

【云栖精选】《云栖精选阿里巴巴技术实战2016年刊》重磅发布

阿里云云栖社区出品的《云栖精选阿里巴巴技术实战2016年刊》整合提炼了云栖社区2016一整年来积累的精华&#xff0c;全书141页。 《云栖精选阿里巴巴技术实战2016年刊》&#xff1a;收藏版、高清版、袖珍版 这里有2016阿里云的大事记&#xff0c;从数加平台正式上线到阿里云品牌…

【云扩RPA】SeperateCol

获取分隔列数据 此节课程将讲述如何使用获取结构化数据组件获取到分隔列数据 准备工作 打开云扩编辑器&#xff0c;新建一个空白项目&#xff0c;搜索序列组件并拖拽至设计面板&#xff0c;设为开始结点,双击打开序列打开Chrome浏览器&#xff0c;输入 “http://www.chinapo…

Spring boot中使用log4j记录日志

为什么80%的码农都做不了架构师&#xff1f;>>> 之前在Spring Boot日志管理 一文中主要介绍了Spring Boot中默认日志工具&#xff08;logback&#xff09;的基本配置内容。对于很多习惯使用log4j的开发者&#xff0c;Spring Boot依然可以很好的支持&#xff0c;只是…

【云扩RPA】SeperateCol_Quiz

获取到整表数据后可以选择不获取整表数据 是否 当检测到多页时&#xff0c;必须要指定下一个按钮吗&#xff1f; 是否 当流程中有绑定浏览器时&#xff0c;运行前必须提前打开指定页面 是否