vue笔记之$attr

含义

$attr是一个对象,它包含了父组件传递给子组件但子组件没有显式声明的props

作用

父亲传数据到孙子

示例

父组件
<template>
    <child :name="name" :age="age" :infoObj="infoObj" />
</template>
<script>
    import Child from '../components/child.vue'

    export default {
        name: 'father',
        components: { Child },
        data () {
            return {
                name: 'Lily',
                age: 22,
                infoObj: {
                    from: '上海',
                    job: 'policeman',
                    hobby: ['reading', 'writing', 'skating']
                }
            }
        }
    }
</script>
子组件
<template>
    <grand-son :height="height" :weight="weight" v-bind="$attrs" />
</template>

<script>
    import GrandSon from '../components/grandSon.vue'
    export default {
        name: 'child',
        components: { GrandSon },
        props: ['name'],
        data() {
            return {
                height: '180cm',
                weight: '70kg'
            };
        },
        created() {
            console.log(this.$attrs);
            // 结果:age, infoObj, 因为父组件共传来name, age, infoObj三个值,由于name被 props接收了,所以只有age, infoObj属性
        }
    }
</script>
孙组件
<template>
    <div>
        {
  { $attrs }}
    <div>
</template>

<script>
    export default {
        props: ['weight'],
        created() {
            console.log(this.$attrs); // age, infoObj, height
        }
    }
</script>

相关推荐

  1. vue笔记$attr

    2024-01-10 07:36:01       65 阅读
  2. Vue中$attrs的作用和使用方法

    2024-01-10 07:36:01       34 阅读
  3. vue笔记$listeners

    2024-01-10 07:36:01       53 阅读
  4. Vue学习笔记侦听器

    2024-01-10 07:36:01       67 阅读
  5. vue爷孙组件传参v-bind=“$attrs“ v-on=“$listeners“

    2024-01-10 07:36:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-10 07:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-10 07:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-10 07:36:01       82 阅读
  4. Python语言-面向对象

    2024-01-10 07:36:01       91 阅读

热门阅读

  1. udp简介4.0

    2024-01-10 07:36:01       59 阅读
  2. zookeeper源码(05)数据存储

    2024-01-10 07:36:01       44 阅读
  3. 小程序适配IOS底部小黑条

    2024-01-10 07:36:01       47 阅读
  4. SQL总结

    2024-01-10 07:36:01       51 阅读
  5. 「 网络安全术语解读 」内容安全策略CSP详解

    2024-01-10 07:36:01       63 阅读
  6. 高并发服务器-多进程

    2024-01-10 07:36:01       63 阅读
  7. tab分页高亮切换及tab交互

    2024-01-10 07:36:01       58 阅读