网站首页 > 博客文章 正文
指的是传递给一个组件,却没有被该组件声明为 props 或 emits 的 attribute 或者 v-on 事件监听器
就像之前说的组件上的style class,
<!-- <MyButton> 的模板 -->
<button>click me</button>
//父组件使用了,并传递了class
<MyButton class="large" v-on=''onclick" />
这是渲染的结果,单根节点
<button class="large" v-on=''onclick">click me</button>
深层组件继承
<!-- <MyButton/> 的模板,只是渲染另一个组件 -->
<BaseButton />
透传的 attribute 不会包含 <MyButton> 上声明过的 props 或是针对 emits 声明事件的 v-on 侦听函数
禁用attribute继承
从v3,3开始,在setup语法糖中这样使用
<script setup>
defineOptions({
inheritAttrs: false
})
</script>
我们想要所有像 class 和 v-on 监听器这样的透传 attribute 都应用在内部的 <button> 上而不是外层的 <div> 上。我们可以通过设定 inheritAttrs: false 和使用 v-bind="$attrs" 来实现:
<div class="btn-wrapper">
<button class="btn" v-bind="$attrs">click me</button>
</div>
<script setup>
defineOptions({
inheritAttrs: false
})
</script>
多根节点的继承
<CustomLayout id="custom-layout" @click="changeValue" />
//由于 Vue 不知道要将 attribute 透传到哪里,所以会抛出一个警告。
<header>...</header>
<main>...</main>
<footer>...</footer>
<header>...</header>
<main v-bind="$attrs">...</main>
<footer>...</footer>
在JavaScript中访问透传Attributes
//Vue3中提供了useAttrsAPI来供我们使用
<script setup>
//如果跟前文的插件装了,可以省略引入
import { useAttrs } from 'vue'
const attrs = useAttrs()
</script>
猜你喜欢
- 2024-09-14 Vue+Element UI实现断点续传、分片上传、秒传
- 2024-09-14 写 Vue 我建议非必要别用 watch(vue不用写html吗)
- 2024-09-14 一文看完vue3的变化之处(vue3稳定吗)
- 2024-09-14 vue中的v-model刨根问底(vue v-model.lazy)
- 2024-09-14 vue的父组件和子组件冲突问题(vue中父组件怎么调用子组件的方法)
- 2024-09-14 「面试题」如何去掉vue的url地址中的#号?及其原理?
- 2024-09-14 Vue进阶(七十一):webpack实现一键动态切换主题色
- 2024-09-14 vue2组件系列第三十七节:Collapse 折叠面板
- 2024-09-14 前端知识点总结——Vue(前端面试题2021及答案vue)
- 2024-09-14 无意中发现 Vue3 的小技巧,帮我节省很多代码
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 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)
本文暂时没有评论,来添加一个吧(●'◡'●)