vue3中$attrs与inheritAttrs的使用

Vue 3 引入了一些新特性和改进,其中之一就是对 $attrs 的处理方式。在 Vue 2 中,$attrs 包含了父组件传递给子组件的属性,但不包括子组件已经声明的 props。在 Vue 3 中,$attrs 的行为有所变化:

  1. 默认情况下,$attrs 包含所有未被子组件的 props 声明的属性 。也就是说,如果父组件传递了多个属性给子组件,即使子组件只声明了其中的一部分作为 props,$attrs 也会包含所有未声明的属性。
javascript 复制代码
<!-- 父组件 -->
<template>
  <MyComponent custom-prop="value" other-attr="otherValue" />
</template>
javascript 复制代码
<!-- 子组件 -->
<template>
  <div>Custom prop: {{ customProp }}</div>
  <!-- $attrs 包含 other-attr="otherValue" -->
</template>

<script setup>
import { defineProps } from 'vue';

const props = defineProps({
  customProp: String
});
</script>
  1. vue3中 $attrs 包含了事件监听器 。在 Vue 2 中,事件监听器不会包含在 $attrs 中。但在 Vue 3 中,如果父组件传递了事件监听器给子组件,这些监听器也会包含在 $attrs 中。
javascript 复制代码
<!-- 父组件 -->
<template>
  <MyComponent @custom-event="handleCustomEvent" />
</template>

<script setup>
const handleCustomEvent() {
    console.log('Custom event triggered');
} 
</script>
javascript 复制代码
<!-- 子组件 -->
<template>
  <button @click="$attrs.onClick">Trigger Event</button>
  <!-- $attrs 包含 @custom-event="handleCustomEvent" -->
</template>

<script setup>
import { useAttrs } from 'vue';

const attrs = useAttrs();
</script>
  1. vue3中 $attrs 是一个响应式对象 。在 Vue 2 中,$attrs 是一个静态对象,不会随着父组件属性的变化而更新。但在 Vue 3 中,$attrs 是响应式的,如果父组件的属性发生变化,$attrs 也会相应地更新。
javascript 复制代码
<!-- 父组件 -->
<template>
  <div>
    <button @click="toggleAttr">Toggle attr</button>
    <MyComponent :dynamic-attr="dynamicAttr" />
  </div>
</template>

<script setup>
import { ref } from 'vue';

const dynamicAttr = ref('initial');

function toggleAttr() {
  dynamicAttr.value = dynamicAttr.value === 'initial' ? 'updated' : 'initial';
}
</script>
javascript 复制代码
<!-- 子组件 -->
<template>
  <div>Dynamic attr: {{ attrs.dynamicAttr }}</div>
</template>

<script setup>
import { useAttrs } from 'vue';

const attrs = useAttrs();
</script>
  1. vue3中 $listeners 已被删除合并到$attrs 。在 Vue 2 中,$listeners 包含了父组件传递给子组件的所有事件监听器。但在 Vue 3 中,$listeners 被移除,所有的属性和事件监听器都包含在 $attrs 中。
javascript 复制代码
<!-- 子组件 -->
<template>
  <button v-on="attrs">Click me</button>
  <!-- 所有事件监听器都会应用到这个按钮上 -->
</template>

<script setup>
import { useAttrs } from 'vue';

const attrs = useAttrs();
</script>
  1. inheritAttrs 选项 。在 Vue 2 中,inheritAttrs 是一个布尔值,用于控制是否将 $attrs 中的属性自动应用到子组件的根元素上。在 Vue 3 中,inheritAttrs 仍然是一个选项,但它现在默认为 false,这意味着属性不会自动应用,除非你明确地使用它们。
javascript 复制代码
<!-- 子组件 -->
<template>
  <div v-bind="$attrs">
    <!-- 属性将应用到这个div上 -->
  </div>
</template>

<script setup>
import { defineProps, withDefaults } from 'vue';

const props = withDefaults(defineProps({}), {});
</script>
  1. v-on="$attrs" 用法 。在 Vue 3 中,你可以使用 v-on="$attrs" 将所有的事件监听器应用到一个元素上。
javascript 复制代码
<!-- 子组件 -->
<template>
  <button v-on="$attrs">Click me</button>
  <!-- 所有事件监听器都会应用到这个按钮上 -->
</template>

<script setup>
// 无需额外的脚本,直接使用$attrs即可
</script>
相关推荐
logstach39 分钟前
QML-Grid和OpacityMask
前端·qml
C+ 安口木1 小时前
前端代码规范 - 日志打印规范
前端·代码规范
朝阳391 小时前
js【最佳实践】遍历数组的八种方法(含数组遍历 API 的对比)for,forEach,for of,map,filter,reduce,every,some
javascript
Jesse_Kyrie2 小时前
配置windows环境下独立浏览器爬虫方案【不依赖系统环境与chrome】
前端·chrome·爬虫·python·scrapy
爱笑的源码基地2 小时前
JAVA妇产科专科电子病历系统源码,前端框架:Vue,ElementUI
vue.js·elementui·前端框架·电子病历系统·源代码·java语言开发·产科管理系统源码
copyer_xyf3 小时前
文件上传
前端·javascript
野槐3 小时前
CSS原子化
前端·css
#学习的路上3 小时前
Bean的管理
java·前端·数据库
香蕉麻花皮3 小时前
vue3 滚动条滑动到元素位置时,元素加载
前端·javascript·vue.js
Fipped3 小时前
Node.js迈向后端世界:从"Hello World"到"Hello Backend!"
前端