vue中 v-bind=“$props“和v-bind=“$attrs“的理解和使用方式

目录

一、v-bind=“$attrs“

二、v-bind="$props"

三、总结


一、v-bind=“$attrs“

主要用于组件之间的隔代传值。 在一个组件里,this.$attr传递当前组件身上绑定的所有属性(不考虑声明了组件内prop的话)

这么说是不是稍微有点绕?让我们看看例子。

// GrandFather.vue    祖父组件
<template>
   <div>
      <father-page :name="testName" :data-name="dataName" />
    </div>
</template>
<script>
    ...
    testName = 'Test101';
    ...
    dataName = 'dataName'
</script>
// FatherPage.vue    父组件
<template>
  <div>
    <child-page v-bind="$attrs" />
  </div>
</template>
<script>
    // 不声明props(不接收祖父组件传过来的数据)
</script>

这时候ChildPage.vue和FatherPage.vue 组件内的 this.$attrs

如果我们修改一下 FatherPage.vue 组件

// FatherPage.vue    父组件
<template>
  <div>
    <child-page v-bind="$attrs" />
  </div>
</template>
<script>
    ...
    props:['testName']
</script>

 

可以看见FatherPage.vue的 this.$attrs 属性里少了testName,而props多了testName

ChildPage.vue接收到的this.$attrs也只有dataName了。

如果使用的是 v-bind="$props" 呢?咱们继续往下看


二、v-bind="$props"

将父组件的所有props下发给它的子组件,子组件需要在其props:{} 中定义要接受的props。

如果将父组件传递给子组件的方式改为

        <grand-son v-bind="$props"></grand-son>

如果在 FatherPage.vue 的props中接收的是testName,那么通过v-bind="$props" 传递给ChildPage.vue 的 this.$attrs 就是testName,如果在 FatherPage.vue中没有声明props,那么在ChildPage.vue 中的this.$attrs 就是空

                        


三、总结

        v-bind="$props"和v-bind="$attrs" 其实就是往子组件里传的参数不同,一个传的是vm.props一个传的是vm.attrs。而组件的attrs其实就是除了本组件声明的prop属性之外的其他绑定在本组件上的属性(可以是vue的动态绑定属性或者name type这些原生/自定义属性)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3使用`v-bind="$attrs"`可以将父组件传递的非props属性传递给子组件。如果你在子组件对这些属性进行了二次封装,你可以通过以下两种方式来调用: 1. 直接使用`$attrs`的属性名来调用 假设你在子组件对`click`事件进行了二次封装: ``` <template> <button @click="handleClick"> {{ text }} </button> </template> <script> export default { props: { text: String, }, methods: { handleClick(event) { // 二次封装的事件处理函数 this.$emit('my-click', event); }, }, }; </script> ``` 在父组件,你可以这样使用: ``` <template> <my-button text="Click Me" v-bind="$attrs" @my-click="handleMyClick" /> </template> <script> import MyButton from './MyButton.vue'; export default { components: { MyButton, }, methods: { handleMyClick(event) { console.log('My Click Event:', event); }, }, }; </script> ``` 注意,你需要使用`@my-click`来监听`MyButton`组件的`$emit('my-click')`事件。 2. 使用`$listeners`来调用 在Vue 3,你还可以通过`$listeners`来访问所有父组件传递给子组件的事件监听器。你可以在子组件使用`$listeners`来继承父组件的事件监听器,然后在组件内部进行二次封装。例如: ``` <template> <button @click="handleClick"> {{ text }} </button> </template> <script> export default { props: { text: String, }, methods: { handleClick(event) { // 二次封装的事件处理函数 this.$emit('my-click', event); }, }, mounted() { // 继承所有父组件传递的事件监听器 Object.keys(this.$listeners).forEach((eventName) => { this.$el.addEventListener(eventName, this.$listeners[eventName]); }); }, }; </script> ``` 在父组件,你可以这样使用: ``` <template> <my-button text="Click Me" v-bind="$attrs" @my-click="handleMyClick" /> </template> <script> import MyButton from './MyButton.vue'; export default { components: { MyButton, }, methods: { handleMyClick(event) { console.log('My Click Event:', event); }, }, }; </script> ``` 注意,你不需要使用`@my-click`来监听`MyButton`组件的`$emit('my-click')`事件,因为`$listeners`已经继承了父组件的事件监听器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值