$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. apache配置文件详解(中英文对照版)

    # This is the main Apache server configuration file. It contains the # configuration directives that ...

  2. day69_10_14 drf接口框架。

    一.drf框架简介 drf全程是:django-rest framework. 其中涉及的知识点有. 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下 ...

  3. monkey和monkeyrunner的区别

    简单来说: 1.monkey是在设备或模拟器直接运行adb shell命令生成随机事件来进行测试 2.monkeyrunner是通过API发送特定的命令和事件来控制设备 为了支持黑盒自动化测试的场景, ...

  4. 【转】理解并设计rest/restful风格接口

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  5. 关于SPFA算法的优化方式

    关于SPFA算法的优化方式 这篇随笔讲解信息学奥林匹克竞赛中图论部分的求最短路算法SPFA的两种优化方式.学习这两种优化算法需要有SPFA朴素算法的学习经验.在本随笔中SPFA朴素算法的相关知识将不予 ...

  6. luoguP3242 [HNOI2015]接水果

    题意 考虑整体二分. 考虑路径\((x,y)\)被路径\((u,v)\)包含需要满足什么条件: 设\(dfn_x\)表示\(x\)的\(dfs\)序,\(low_x=dfn_x+size_x-1\), ...

  7. Paper | Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising

    目录 故事背景 网络结构 BN和残差学习 拓展到其他任务 发表在2017 TIP. 摘要 Discriminative model learning for image denoising has b ...

  8. IPv6 地址生命周期

    在preferred time和valid lifetime之间叫做deprecated 状态,当地址达到这个时间段的时候,地址不能主动的发起连接只能是被动的接受连接,过了valid lifetime ...

  9. (十八)golang--defer关键字

    在函数中,程序员经常需要创建资源(比如,数据库连接,文件句柄,锁等),为了在函数执行完毕后,及时释放资源,go设计者提供defer(延时机制) 用defer申明的语句不会立即执行,而是被存入到defe ...

  10. 机器学习(六)--------神经网络(Neural Networks)

    无论是线性回归还是逻辑回归都有这样一个缺点,即:当特征太多时, 计算的负荷会非常大. 比如识别图像,是否是一辆汽车,可能就需要判断太多像素. 这时候就需要神经网络. 神经网络是模拟人类大脑的神经网络, ...