$attrs/inheritAttrs可以实现组件的跨级传递
 // 问题1  为什么this.$attrs可以得到主  传递过来的值
        //$attrs 说明 
        // $attrs 可以很方便的做到属性透传,使用起来也比较简单,避免了多写 props 的痛苦。
        // 当一个组件没有声明任何prop时候,attrs里面包含着全部的上层组件传递的所有数据(除style和class)
        // 当一个组件声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。
        // inheritAttrs
        // 1.当在子组件中设置inheritAttrs: false的时候,attrs里面的属性不会当做html的data属性渲染在dom节点之上。
        // 2.在子组件中不进行设置inheritAttrs的时候,attrs里面的属性会渲染在html节点之上
        // 3.当设置为inheritAttrs: false的时候,在组件的声明周期created中可以通过 this.$attrs 获取里面的上层组件数据。
        // 当在子组件中设置inheritAttrs: false的时候,attrs里面的属性是没有style和class的
 
 
 // $attrs/$listeners可以跨级传递  兄弟之间就不行
 // 最大的go组件里面引入go1组件
 // go1组件里面有go2组件
 // go2里面有go3组件
 
go.vue
  1. <template>
  2. <div>
  3. 我是 go
  4. <go1
  5. :foo="foo"
  6. :boo="boo"
  7. :coo="coo"
  8. :doo="doo"
  9. ></go1>
  10. </div>
  11. </template>
  12.  
  13. <script>
  14. import go1 from "../go1/go1"
  15. export default {
  16. data(){
  17. return{
  18. foo: "Javascript",
  19. boo: "Html",
  20. coo: "CSS",
  21. doo: "Vue"
  22. }
  23. },
  24.  
  25. components:{
  26. go1
  27. }
  28. }
  29. </script>
 
 go1.vue
  1. <template>
  2. <div>
  3. <h2>-----------------</h2>
  4. <br>
  5. 我是go1111
  6. <!-- <p>foo: {{ foo }}</p> -->
  7. <!-- <p>go1得到主组件中的数据$attrs: {{ $attrs }}</p> -->
  8. <go2 v-bind="$attrs"></go2>
  9. <br>
  10. <h2>-----------------</h2>
  11.  
  12. </div>
  13. </template>
  14.  
  15. <script>
  16. import go2 from "../go2/go2"
  17. export default {
  18. components:{
  19. go2
  20. },
        inheritAttrs: false, // .当设置为inheritAttrs: false的时候,在组件的声明周期中可以通过 this.$attrs 获取里面的上层组件数据。
  1.  
  1. props: {
  2. foo: String // 当声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。 所以下面没有 Javascript
  1. },
  2.  
  3. created() {
  4. console.log("我在1出输出",this.$attrs); // {boo: "Html", coo: "CSS", doo: "Vue"}
  5. }
  6.  
  7. }
  8. </script>
 
 go2.vue
  1.  
<template>
    <div> 
        <h2>-----------------</h2>
        <br>
        
        go222
        <p>boo: {{ boo }}</p>
        <p>childCom2: {{ $attrs }}</p>
        <go3 v-bind="$attrs"></go3>
        <br>
        <h2>-----------------</h2>
    </div>
</template>
<script>
import go3 from "../go3/go3"
    export default {
         components:{
             go3
         },
         inheritAttrs: false,
         props: {
             boo: String  //当声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。 所以下面没有 html
         },
         created() {
            console.log("我是组件2",this.$attrs); // 我是组件2 {coo: "CSS", doo: "Vue"}
         }
    }
</script>
 go3.vue
  1.  
<template>
    <div> 
        <h2>-------</h2>
        <br>
        go333
           <p>childCom3: {{ $attrs }}</p>
        <h2>-------</h2>
        <br>
    </div>
</template>
<script>
    export default {
        props: {
            coo: String,  //当声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。 所以下面没有 coo
        },
         created() {
            console.log("我是组件3",this.$attrs); // 我是组件2 { doo: "Vue"}
         }
    }
</script>
  1.  

随机推荐

  1. Python3面向对象编程实例

    /*认真研究完这个实例,理解python面向对象编程的方法*/class Person: """人员信息""" # 姓名(共有属性) nam ...

  2. C++ 模板特化、偏特化测试程序

    #include <iostream> // 偏特化的模板不会自己添加构造函数 ctor 和 析构函数 dtor #if 1 // P1 template <typename T1, ...

  3. SQL Server 迁移数据库 (三)使用SQL脚本

    1. 创建脚本 1.1 在需要迁移的数据库上右击>Tasks>Generate Scrips 1.2 前两步直接Next,第三步我这里选择Save to Clipboard,因为如果选Sa ...

  4. Vue 事件的基本使用与语法差异

    "v-on:"可以简写为"@" "click"单击 "dblclick"双加 代码: <!doctype html ...

  5. x2

    #include<stdio.h> int main () { int days; printf("输入一个整数:\n"); scanf("%d", ...

  6. Git修改和配置用户名和邮箱

    git在push/push to时需要使用到user.name和user.email,切记一定要现配置好查看user.name/user.email git config user.name git ...

  7. Java连载36-IDE使用

    一.主方法注意 每一个类都可以编写一个主方法,但是一般情况下,一个系统只有一个入口,所以主方法一般写一个 二.Myeclipse的使用 1.在workspace中工作区中有一个文件夹.metadata ...

  8. Python连载23-file_analysis

    一.文件 1.定义:长久保存信息的一种信息集合 2.常用操作:(1)打开关闭(2)读写内容(3)查找 3.open函数 (1)意义:打开文件,带有很多参数 (2)第一个参数:必须有,文件的路径和名称 ...

  9. LeetCode 328:奇偶链表 Odd Even Linked List

    ​给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1), ...

  10. 2019-11-29-WPF-禁用实时触摸

    原文:2019-11-29-WPF-禁用实时触摸 title author date CreateTime categories WPF 禁用实时触摸 lindexi 2019-11-29 10:20 ...