vue中使用v-bind=“$attrs“进行多层组件的传值

本文介绍了在Vue中如何利用v-bind='$attrs'和v-on='$listeners'实现多层组件间的传值和事件监听。在grandfather组件中传递props到father,然后father将未接收的props通过v-bind='$attrs'传递给grandson。同时,讨论了vm.$attrs和vm.$listeners的作用,vm.$attrs包含父作用域中未作为props的属性绑定,而vm.$listeners则包含父组件的事件监听器,可用于创建高级别组件时传递事件。
摘要由CSDN通过智能技术生成

有如下的嵌套关系

<grandfather>
	<father>
		<grandson>
		</grandson>
	</father>
</grandfather>
  • 比如说在grandfather中传给了father一些props

grandfather.vue

<div>
	<father
	name="test"
	age="18"
	hobby="badminiton"
	grade="3"
	>
	</father>
</div>
  • 然后在father接收一些想要的值

father.vue

<div>
	<grandson v-bind="$attrs">
	</grandson>
</div>


props: {
      name: {
        type: String,
        default: 'default'
      },
      age: {
        type: String,
        default: 'default'
      }
    }
注意这里并没有接收hobby和grade这两个值
那么这两个未被father接收的值就可以通过v-bind="$attrs"
传递给grandson了
  • grandson接收一些father没有接受的值

    props:{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值