$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. VMware workstation安装centos7,无ifconfig命令

    一.centos7默认未启动ifconfig,vi /etc/sysconfig/network-scripts/ifcfg-ens33,改成ONBOOT=yes: systemctl restart ...

  2. pyplot中的一些函数

    from matplotlib import pyplot as plt plt.ylabel(‘Grade’) : y轴的名称 plt.xlabel(‘Grade’) : x轴的名称 plt.tit ...

  3. python27期前端

    第一天笔记:HTML:是一种标识性的语言css:层叠样式表是一种用来表现HTML等文件样式(效果)的计算机语言JavaScript:简称“JS”,是一种属于网络的脚本语言 常用来为网页添加各式各样的动 ...

  4. PKCS pfx cer x509

    PKCS pfx cer x509 参考 PKCS 15 个标准 PKCS The Public-Key Cryptography Standards (PKCS)是由美国RSA数据安全公司及其合作伙 ...

  5. Linux下MongoDB安装和配置(二)

    1. 下载MongoDB 下载地址:https://www.mongodb.com/download-center/community 这里选择的是:mongodb-linux-x86_64-4.0. ...

  6. Chatbot:

    讲清了一些最最基本的概念 - Intents(意图)和Entities(关键字) - 基于意图(Intent-based)的对话 和 基于流程(Flow-based)的对话  聊天机器人教学:使用Di ...

  7. WEB 中的文件下载(待修改、完善)

    在 WEB 开发中,我们会期望用户在点击某个链接的时候,下载一个文件(不管这个文件能不能被浏览器解析,都要下载).以前接触过一种方式,就是在响应 header 中设置 force-download : ...

  8. git--github使用

    什么是github GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. GitHub于2008年4月10日正式上线,除了Git代码仓 ...

  9. strcasecmp()函数

    函数介绍: strcasecmp用忽略大小写比较字符串.,通过strcasecmp函数可以指定每个字符串用于比较的字符数,strncasecmp用来比较参数s1和s2字符串前n个字符,比较时会自动忽略 ...

  10. java4wifidog_server_README

    项目地址:https://github.com/C-hill/java4wifidog_server 开发环境:Windows  JDK7  Tomcat6  Myeclipse8.5  MySQL5 ...