网站首页 > 博客文章 正文
透传属性 (透传 attribute)
什么是透传属性(透传 attribute)? 传递给一个组件,却没有被该组件声明为 props 或 emits 的 attribute 或者是事件监听器,例如 class style id 等。
属性继承
当一个组件以单个元素作渲染时,透传的属性会自动被添加到根元素上。
// 组件
<script lang="ts" setup>
import { ref } from 'vue'
</script>
<template>
<div class="container">
<h1>这是深入组件-属性透传</h1>
</div>
</template>
<style lang="scss" scoped>
.container {
}
</style>
// 父组件
<script lang="ts" setup>
import { ref } from 'vue'
import Com16 from '@/components/demo/Com16.vue'
</script>
<template>
<div class="container">
<Com16 class="big"></Com16>
</div>
</template>
<style lang="scss" scoped>
.container {
}
</style>
对 class 和 style 的合并
如果一个子组件的根元素已经有了 class 或 style attribute,它会和从父组件上继承的值合并。
// 子组件中
<h1 class="h111">这是深入组件-属性透传</h1>
同样的规则也适用于 v-on 事件监听器
当父子组件都定义了事件则都会出发,并且是从内到外相应
//子组件
<script lang="ts" setup>
import { ref } from 'vue'
const fun1 = () => {
console.log('我是组件事件')
}
</script>
<template>
<div class="container h1">
<h1 @click="fun1">这是深入组件-属性透传</h1>
</div>
</template>
<style lang="scss" scoped>
.container {
}
</style>
//父组件
<script lang="ts" setup>
import { ref } from 'vue'
import Com16 from '@/components/demo/Com16.vue'
const fun2 = () => {
console.log('我是父组件事件')
}
</script>
<template>
<div class="container">
<Com16 @click="fun2" class="big"></Com16>
</div>
</template>
<style lang="scss" scoped>
.container {
}
</style>
禁用 Attributes 继承
如果你不想要一个组件自动地继承 attribute,你可以在组件选项中设置
inheritAttrs: false。
<script lang="ts" setup>
defineOptions({
inheritAttrs: false
})
import { ref } from 'vue'
const fun1 = () => {
console.log('我是组件事件')
}
</script>
确实 class 没有 big 了
这些透传进来的 attribute 可以在模板的表达式中直接用 $attrs 访问到。
template Fallthrough attribute: {{ $attrs }} 这个 $attrs 对象包含了除组件所声明的 props 和 emits 之外的所有其他 attribute,例如 class,style,v-on 监听器等等。
注意:
- 和 props 有所不同,透传 attributes 在 JavaScript 中保留了它们原始的大小写,所以像 foo-bar 这样的一个 attribute 需要通过 $attrs['foo-bar'] 来访问。
- 像 @click 这样的一个 v-on 事件监听器将在此对象下被暴露为一个函数 $attrs.onClick。
多根节点的 Attributes 继承
和单根节点组件有所不同,有着多个根节点的组件没有自动 attribute 透传行为。如果 $attrs 没有被显式绑定,将会抛出一个运行时警告。
在 JavaScript 中访问透传 Attributes
如果需要,你可以在 <script setup> 中使用 useAttrs() API 来访问一个组件的所有透传 attribute:
<script setup>
import { useAttrs } from 'vue'
const attrs = useAttrs()
</script>
需要注意的是,虽然这里的 attrs 对象总是反映为最新的透传 attribute,但它并不是响应式的 (考虑到性能因素)。
猜你喜欢
- 2024-10-27 Vue 3 大刀阔斧:告别旧爱,拥抱更简洁高效的新语法!
- 2024-10-27 深入剖析Vue源码 - 你了解v-model的语法糖吗?
- 2024-10-27 Vue3,Composition API(组合) setup、ref、reactive、toRefs案例
- 2024-10-27 Vue3数据传递$attrs(二)(vue三种传值方式)
- 2024-10-27 Vue2 到 Vue3,重学这 5 个常用的 API
- 2024-10-27 vue内置组件、特殊元素、组件api详解
- 2024-10-27 Vue 3最常用的函数或指令(备用查询)
- 2024-10-27 Vue 3新的API——Composition API组合函数(三)
- 2024-10-27 Vue3 - $attrs 的几种用法(1个或多个根元素、Vue3的两种语法)
- 2024-10-27 想知道Vue3与Vue2的区别?五千字教程助你快速上手Vue3
你 发表评论:
欢迎- 06-23MySQL合集-mysql5.7及mysql8的一些特性
- 06-23MySQL CREATE TABLE 简单设计模板交流
- 06-23MYSQL表设计规范(mysql设计表注意事项)
- 06-23MySQL数据库入门(四)数据类型简介
- 06-23数据丢失?别慌!MySQL备份恢复攻略
- 06-23MySQL设计规范(mysql 设计)
- 06-23MySQL数据实时增量同步到Elasticsearch
- 06-23MySQL 避坑指南之隐式数据类型转换
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)