vue3+ts深入组件(三)透传Attributes

一、含义

本节内容作为父子组件传参的补充内容
掌握程度 :了解

透传Attributes ,一般用来在父子组件中传递属性例如,class,style,id…
且该值不会被组件当作prop或者emits。透传Attributes,也可以用来传递v-on事件监听器
透传的参数一般会直接传递给子组件的根元素(例外情况下文讲解)

例如:

1.父组件中使用,并传入一个class
<Cpn class="parent"/>

渲染后的DOM元素=>
2.子组件只有一个根元素button中
<div class="parent"></div>
 
 即子组件中只有一个根元素时,直接将class传入唯一的根元素中

如果根元素本来就存在class,将会 合并 !!
class=“parent children”

二、v-on事件监听器继承及深层组件透传

1.v-on事件监听器继承

click 监听器会被添加到根元素,即那个原生的根元素之上。当原生的根元素被点击,会触发父组件的 onClick 方法。同样的,如果原生 button 元素自身也通过 v-on 绑定了一个事件监听器,则这个监听器和从父组件继承的监听器都会被触发。

<Cpn @click="parentClick" />
渲染后的DOM元素=>
<button @click="parentClick"  @click="childClick"></button>
此时点击该按钮,将触发两个事件监听器即这个监听器和从父组件继承的监听器都会被触发。

2.深层组件透传

当子组件的根元素是另一个 组件,则会直接将父组件透传的Attributes直接传递给“孙子”组件
特别的,当透传的Attributes也符合Props声明,也可以通过Props传递给"孙子"组件

父组件
<Cpn class="parent"/>

子组件
<Base />
"孙子"组件
<div class="parent">

三、如何禁用attributes继承

使用 inheritAttrs: false来将透传attributes继承禁用
<scipt setup>语法糖模式下
需要单独写一个scipt块

<script>
export default {
  inheritAttrs: false
}
</script>

<script setup>
</script>

父组件透传Attributes后这些透传进来的 attribute 可以在模板的表达式中直接用 $attrs 访问到。这个$attrs 对象包含了除组件所声明的 props 和 emits 之外的所有其他 attribute
例如 class,style,@ 监听器等等。和单根节点组件有所不同,有着多个根节点的组件没有自动 attribute 透传行为。需要将 $attrs 显式绑定,否则将会抛出一个运行时警告。

<span> attribute: {{ $attrs }}</span>

这个 $attrs 对象包含了除组件所声明的 props 和 emits 之外的所有其他 attribute,例如 class,style,v-on 监听器等等。

需要访问attribute时, 需要通过 $attrs[‘attribute’] 来访问。

像 @click 这样的一个 v-on 事件监听器将在此对象下被暴露为一个函数 $attrs.onClick。

有时候我们可能为了样式,需要在根元素元素外包装一层
我们可以通过设定 inheritAttrs: false 和使用 v-bind=“$attrs” 来实现:

<div class="contentr">
  <button class="btn" v-bind="$attrs">click me</button>
</div>

四、多个根节点的透传Attributes

父组件中
<Cpn  class="Content">

子组件中
<header>...</header>
<main v-bind="$attrs">...</main>
<footer>...</footer>

文末
感谢阅读,还望支持!欢迎学习交流,如有错误,还请指正!
已更新于2022/11/26

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3是一种流行的JavaScript框架,而TypeScript是一种类型安全的JavaScript超集。结合使用Vue3和TypeScript可以提供更好的开发体验和代码可维护性。 在Vue3中,可以使用TypeScript来编写组件。下面是一个简单的Vue3+TypeScript评论组件的示例: ```typescript <template> <div> <h2>评论列表</h2> <ul> <li v-for="comment in comments" :key="comment.id"> {{ comment.text }} </li> </ul> <form @submit.prevent="addComment"> <input type="text" v-model="newComment" placeholder="请输入评论" /> <button type="submit">添加评论</button> </form> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface Comment { id: number; text: string; } export default defineComponent({ name: 'CommentComponent', data() { return { comments: [] as Comment[], newComment: '', }; }, methods: { addComment() { const newId = this.comments.length + 1; this.comments.push({ id: newId, text: this.newComment }); this.newComment = ''; }, }, }); </script> ``` 在上面的示例中,我们定义了一个名为`CommentComponent`的Vue组件组件包含一个评论列表和一个表单用于添加新的评论。`comments`数组用于存储评论数据,`newComment`变量用于绑定输入框的值。 通过使用`defineComponent`函数和`ref`函数,我们可以定义组件的数据和方法,并将其导出供其他组件使用。 相关问题: 1. Vue3是什么? 2. TypeScript在Vue3中的作用是什么? 3. 请解释一下Vue3+TypeScript评论组件的代码逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Asen-coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值