Vue中的爷孙传值$attrs 和利用$listeners 孙爷传值

混沌寄存器
• 阅读 8994

所谓的$arrts其实就是多级组件中的props,它就像一个中间件,用来传递爷组件给孙组件的数据,使用的时候只需给父组件中的孙组件配置v-bind="$attrs",然后再爷组件中传入孙组件所需要的数据,孙组件正常接收即可。
参考于

什么情况下使用

在实际开发过程中,会经常出现组件多级嵌套的情况,那么这个时候如最下图所示,需要组件A传递数据给组件C,通常可以使用props依次传递 和 Vuex(状态管理)来实现,但是这两种方式要么代码臃肿不堪,要么大材小用,而这个时候$attrs就可以很好的解决这个问题。
$attrs是Vue实例的属性。

具体见下文demo

componentA

<template\>

<div  class\="dv"\>

我是组件A

<componentB  text\='ttt'  msg\="yeye"\></componentB\>

</div\>

</template\>

  

<script\>

import  componentB  from  '@/componentB'

export  default {

components: {

componentB

}

}

</script\>

  

<style  scoped>

.dv{

width: 600px;

height: 600px;

background-color: yellow;

}

</style\>
componentB
<template\>

<div  class\="dv"\>

我是B

<span\>传递给我的是{{text}}</span\>

<componentC  v-bind\="$attrs"\></componentC\>

</div\>

</template\>

  

<script\>

import  componentC  from  '@/componentC'

export  default {

components: {

componentC

},

props: {

text: {

type: String,

default: ''

}

}

  

}

</script\>

  

<style  scoped>

.dv{

width: 400px;

height: 400px;

background-color: pink;

}

</style\>

componentC

<template\>

<div  class\="dv"\>

我是c

<span\>我要爷爷的{{msg}}</span\>

</div\>

</template\>

  

<script\>

export  default {

props: {

msg: {

type: String,

default: ''

}

}

}

</script\>

  

<style  scoped>

.dv{

width: 200px;

height: 200px;

background-color: red;

}

</style\>
效果

Vue中的爷孙传值$attrs 和利用$listeners 孙爷传值

$listeners

grandFather.vue

<template>
    <div>
        <father :age="ag" :number="nu" @onChange="onChange"></father>
    </div>
</template>

<script>
import father from './father'
    export default {
        components:{
            father
        },
         data() {
            return {
                ag: 12,
                nu:111,
            }
        },
        methods: {
            onChange(ha){
                console.log(ha)//'传给爷爷的'
            }
        },
    }
</script>

father.vue

<template>
  <div>
    <son v-bind="$attrs" v-on='$listeners'></son>
  </div>
</template>

<script>
import son from './son.vue'
export default {
  props: {
    value: {
      type: Number,
    },
  },
  components:{
      son
},
created(){
  console.log(this.$attrs)//age number
  console.log(this.$listeners)//onChange
}
}
</script>

<style lang="scss" scoped></style>

son.vue

<template>
    <div>
        {{age}}
    </div>
</template>

<script>
    export default {
        props:['age'],
        created(){
            console.log(this.$attrs) // 111 因为父组件共传来 age, number两个值,由于age被 props接收了,所以只有number, 
            this.$emit('onChange','传给爷爷的')
        }
    }
</script>

<style lang="scss" scoped>

</style>

$attrs:包含了父作用域中不被认为 (且不预期为) props 的特性绑定 (class 和 style 除外),并且可以通过 v-bind=”$attrs” 传入内部组件。当一个组件没有声明任何 props 时,它包含所有父作用域的绑定 (class 和 style 除外)。

$listeners:包含了父作用域中的 (不含 .native 修饰符) v-on 事件监听器。它可以通过 v-on=”$listeners” 传入内部组件。它是一个对象,里面包含了作用在这个组件上的所有事件监听器,相当于子组件继承了父组件的事件。

点赞
收藏
评论区
推荐文章
Alex799 Alex799
4年前
vue最新面试题
最近在面试,总结几个重点的面试题:一、vue父子组件之间的传值:简单来说,子组件通过props方法接受父组件传来的值,子组件通过$emit方法来向父组件发送数据。(具体案例可以看我之前写的博客)。二、vue生命周期函数:beforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedbeforeDestr
Chase620 Chase620
4年前
vue的8种通信方式
1.props/emit1.父组件向子组件传值下面通过一个例子说明父组件如何向子组件传递数据:在子组件article.vue中如何获取父组件section.vue中的数据articles:\'红楼梦','西游记','三国演义'\//section父组件<template<divclass"section"
爱丽丝13 爱丽丝13
4年前
React 组件通信之发布订阅模式
React通信react的数据流是单向的,react通信有以下几种方式:父向子通信:传入props子向父通信:父组件向子组件传一个函数,然后通过这个函数的回调,拿到子组件传过来的值父向孙通信:利用context传值。React.createContext()兄弟间通信:​1、找一个相同的父组件,既可以用pr
maxbad maxbad
4年前
vue2 $attrs/$listeners
包含了⽗作⽤域中不作为prop被识别(且获取)的特性绑定(class和style除外)。当⼀个组件没有声明任何prop时,这⾥会包含所有⽗作⽤域的绑定(class和style除外),并且可以通过vbind"$attrs"传⼊内部组件——在创建⾼级别的组件时⾮常有⽤。//child:并未在props中声明foo$attrs
Chase620 Chase620
4年前
Vue方向:prop验证
我们可以为组件的prop指定验证规则,如果传入的数据不符合要求,Vue会发出警告,这对于开发给他人使用的组件非常有用。要指定验证规则,需要用对象的形式来定义prop,而不能用字符串数组;1、验证父组件传递过来的数据类型props选项值如果为数据,则表示只是单纯的罗列父组件传递过来的数据而已如果props选项的值是一个对象,那么表示要验证接收的
Dax Dax
4年前
Vue父子组件
几种常见的通信方式:1、prop属性父组件通过绑定属性的方式,给子组件传值,同时子组件通过设置props来接收letChildVue.extend(template:'content',props:content:type:String,default:()r
Chase620 Chase620
4年前
关于:父组件中的子组件中的子组件(孙级)添加数据后怎么样去调用父组件中的方法呢?
1、需求引入image.png这是父组件下的子组件的子组件,一个绑定子设备的弹出窗modal,那么我们怎么样在这个组件中选中子设备后点击确定时去触发父组件的父组件的table列表数据刷新呢?image.pngimage.pngimage.png组件的层级关系.png上述的图示便是部分代码的展示,一般的组件传值有父组件传子组件(
Stella981 Stella981
3年前
Flutter子组件调用父组件方法修改父组件参数
子组件调用父级组件方法的主要实现是父组件给子组件传入一个方法,然后在子组件中调用父级方法来修改父级的参数。看一下效果图!(https://img2018.cnblogs.com/blog/1038765/201907/103876520190702152130487420290106.gif)父级组件实现在父级组件中写一个
Stella981 Stella981
3年前
React.js 时间组件 + 组件生命周期(更新模拟)
React是用于构建用户界面的JavaScript库,React组件使用一个名为render()的方法,接收数据作为输入,输出页面中对应展示的内容。React除了可以使用外部传入的数据以外(通过this.props访问传入数据),组件还可以拥有其内部的状态数据(通过this.state访问状态数据)。当组件的状态
Stella981 Stella981
3年前
FLutter父子组件通信
本文介绍flutter父子组件数据传递和回调.还是以之前的代码为例Flutter\_DayByDay(https://gitee.com/Royce_he/Flutter_DayByDay)由于之前用ReactNative写项目,顺便对比一下RN父组件直接在xml标签中写属性{值/方法},子组件通过props去取属性和方法
可莉 可莉
3年前
04. react 初次见面
    组件从概念上看就像是函数,它可以接收任意的输入值(称之为“props”),并返回一个需要在页面上展示的React元素。1、组件定义的两种方式1.1函数定义组件  定义一个组件最简单的方式是使用JavaScript函数:functionWelcome(props){return<