Vue3 组件化开发(5)——组件间的通信(3)

149 阅读1分钟

「这是我参与2022首次更文挑战的第27天,活动详情查看:2022首次更文挑战」。

propattribute

什么是非 propattribute 呢?

  • 当我们传递给一个组件某个属性,但是该属性并没有在 propsemits 中进行相应地定义时,我们就称该属性为propattribute
  • 常见的例子包括 classstyleid 属性等;

我们可以通过 $attrs 属性(property)访问那些非 prop 的属性(attribute)。

我们知道,class 是原生 HTML 元素(如 <div>)上都有的属性(attribute),但是在我们自定义的 Vue 组件上原本可没有 class 属性(attribute),所以如果在一个 Vue 组件上传递了 class 属性,那么默认情况下(没有在该组件中定义这个 class)这个 class 属性就会成为非 propattribute

那么,如果我们给一个组件传递了一个非 propattributeVue 会分成 3 种情况来处理:

  1. 默认情况,即组件只有一个根节点时;
  2. 禁用 attribute 继承时;
  3. 存在多个根节点时;

attribute 继承

组件有单个根节点时,propattribute 将自动添加到根节点的 attribute

举个例子:

假如 ShowMessage.vue 组件中的模板是这样的:

<template>
  <div>
    <h2>{{ title }}</h2>
    <p>{{ content }}</p>
  </div>
</template>

这时 ShowMessage.vue 组件就只有一个根节点(<div>),那么在 App.vue 组件中使用 ShowMessage.vue 组件时,如果传入了 class 属性:

image-20220226211153835

那么最后的效果是这个 class 属性会被添加到 ShowMessage.vue 组件的根节点上:

image-20220103194404903

所以,对于单个根节点的组件,其根节点会默认继承传过来的非 propattribute

禁用 attribute 继承

但有时候我们可能不希望组件的根元素自动继承 attribute,那么我们可以在组件中设置 inheritAttrs: false

image-20220226210507709

image-20220226210545247

  • 禁用 attribute 继承的常见情况需要将 attribute 应用于根元素之外的其它元素
  • 我们可以通过 $attrs 来访问所有的非 propsattribute

image-20220226210930971

image-20220226211007491

当然,如果有多个 attribute 传入,比如除了 class,还有 id

image-20220226211443462

那么我们除了可以一个一个绑定:

image-20220226212052205

还可以使用 v-bind 一次性全部绑定:

image-20220226212207715

两种方式的效果是一样的:

image-20220226212246376

多个根节点上的 attribute 继承

多个根节点的 attribute 如果没有显式地绑定:

image-20220226213842937

那么控制台会报警告:

image-20220226213528068

我们必须在目标元素上进行手动绑定:

image-20220226213903753

image-20220226213929421