$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
<template>
<div>
我是 go
<go1
:foo="foo"
:boo="boo"
:coo="coo"
:doo="doo"
></go1>
</div>
</template> <script>
import go1 from "../go1/go1"
export default {
data(){
return{
foo: "Javascript",
boo: "Html",
coo: "CSS",
doo: "Vue"
}
}, components:{
go1
}
}
</script>
 
 go1.vue
<template>
<div>
<h2>-----------------</h2>
<br>
我是go1111
<!-- <p>foo: {{ foo }}</p> -->
<!-- <p>go1得到主组件中的数据$attrs: {{ $attrs }}</p> -->
<go2 v-bind="$attrs"></go2>
<br>
<h2>-----------------</h2> </div>
</template> <script>
import go2 from "../go2/go2"
export default {
components:{
go2
},
        inheritAttrs: false, // .当设置为inheritAttrs: false的时候,在组件的声明周期中可以通过 this.$attrs 获取里面的上层组件数据。

        props: {
foo: String // 当声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。 所以下面没有 Javascript
        },

        created() {
console.log("我在1出输出",this.$attrs); // {boo: "Html", coo: "CSS", doo: "Vue"}
} }
</script>
 
 go2.vue

<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

<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. 3. Vue - 指令系统

    一.vue指令 (1) v-if // 条件判断 如果条件成立就在页面上生成一个标签并显示出来 (2) v-show //DOM中都存在只是显示与否 (3) v-for //注意 v-bind:key ...

  2. tarjan 缩点 + 几道例题

    tarjan 缩点 + 几道例题 tarjan 模板 #include <iostream> #include <string.h> using namespace std; ...

  3. C++中的异常处理(中)

    为什么要在catch中重新抛出异常? #include <iostream> #include <string> using namespace std; void Demo( ...

  4. miniapp之登录、授权和支付

    微信小程序代码实现(登录.授权和支付) ==整体流程看上一篇博客,或者去微信公众平台查看文档== ==只列出核心代码,详细代码见码云michaelben== 登录 // //小程序端 // app.j ...

  5. JDOJ3004 超级楼梯

    JDOJ3004 超级楼梯 https://neooj.com/oldoj/problem.php?id=3004 题目描述 有一个超级楼梯共N级,刚开始时你在第一级,若每次只能跨上一级或两级,要走上 ...

  6. dirb参数解析

    -----------------DIRB v2.22 By The Dark Raver----------------- dirb <url_base> [<wordlist_f ...

  7. RocketMQ支持事务消息机制

    事务消费 我们经常支付宝转账余额宝,这是日常生活的一件普通小事,但是我们思考支付宝扣除转账的钱之后,如果系统挂掉怎么办,这时余额宝账户并没有增加相应的金额,数据就会出现不一致状况了. 上述场景在各个类 ...

  8. Start LaTex

    目录 Size Color Shape Common Function Type Fill Label Beamer Example Size You can use: ultra thin , ve ...

  9. iOS13 新特性简介

    目录 一.Dark Mode 暗黑模式 二.Status Bar更新 三.UIActivityIndicatorView加载视图 四.总结 一.Dark Mode 暗黑模式 1.1 iOS13推出了D ...

  10. Mongodb的常用语句

    模糊查询  regex(".*?\\"+questionContent+".*") String questionContent = "需求" ...