【vue3】数据绑定,动态渲染class与style

数据绑定的一个常见需求场景是操纵元素的 CSS class 列表和内联样式(style)。因为 class 和 style 都是 attribute,我们可以和其他 attribute 一样使用 v-bind 将它们和动态的字符串绑定。但是,在处理比较复杂的绑定 时,通过拼接生成字符串是麻烦且易出错的。因此,Vue 专门为 class 和 style 的 v-bind 用法提供了特殊的功能增强除了字符串外,表达式的值也可以是对象或数组。

绑定 class

绑定对象

我们可以给 :class (v-bind:class 的缩写) 传递一个对象来动态切换 class:

html 复制代码
<div :class="{ active: isActive }"></div>

上面的语法表示 active 是否存在取决于数据属性 isActive 的真假值。

你可以在对象中写多个字段来操作多个 class。此外,:class 指令也可以和一般的 class attribute 共存。举例来说,下面这样的状态:

javascript 复制代码
data() {
  return {
    isActive: true,
    hasError: false
  }
}

配合以下模板:

html 复制代码
<div
  class="static"
  :class="{ active: isActive, 'text-danger': hasError }"
></div>

渲染的结果会是:

html 复制代码
<div class="static active"></div>

当 isActive 或者 hasError 改变时,class 列表会随之更新。举例来说,如果 hasError 变为 true,class 列表也会变成 "static active text-danger"。

绑定的对象并不一定需要写成内联字面量的形式,也可以直接绑定一个对象:

javascript 复制代码
data() {
  return {
    classObject: {
      active: true,
      'text-danger': false
    }
  }
}
html 复制代码
<div :class="classObject"></div>

这也会渲染出相同的结果。我们也可以绑定一个返回对象的计算属性。这是一个常见且很有用的技巧:

javascript 复制代码
data() {
  return {
    isActive: true,
    error: null
  }
},
computed: {
  classObject() {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal'
    }
  }
}
html 复制代码
<div :class="classObject"></div>

绑定数组

我们可以给 :class 绑定一个数组来渲染多个 CSS class:

javascript 复制代码
data() {
  return {
    activeClass: 'active',
    errorClass: 'text-danger'
  }
}
html 复制代码
<div :class="[activeClass, errorClass]"></div>

渲染的结果是:

html 复制代码
<div class="active text-danger"></div>

如果你也想在数组中有条件地渲染某个 class,你可以使用三元表达式:

html 复制代码
<div :class="[isActive ? activeClass : '', errorClass]"></div>

errorClass 会一直存在,但 activeClass 只会在 isActive 为真时才存在。

然而,这可能在有多个依赖条件的 class 时会有些冗长。因此也可以在数组中嵌套对象:

html 复制代码
<div :class="[{ active: isActive }, errorClass]"></div>

在组件上使用

对于只有一个根元素的组件,当你使用了 class attribute 时,这些 class 会被添加到根元素上,并与该元素上已有的 class 合并。

举例来说,如果你声明了一个组件名叫 MyComponent,模板如下:

html 复制代码
<!-- 子组件模板 -->
<p class="foo bar">Hi!</p>

在使用时添加一些 class:

html 复制代码
<!-- 在使用组件时 -->
<MyComponent class="baz boo" />

渲染出的 HTML 为:

html 复制代码
<p class="foo bar baz boo">Hi!</p>

Class 的绑定也是同样的:

html 复制代码
<MyComponent :class="{ active: isActive }" />

当 isActive 为真时,被渲染的 HTML 会是:

html 复制代码
<p class="foo bar active">Hi!</p>

如果你的组件有多个根元素,你将需要指定哪个根元素来接收这个 class。你可以通过组件的 $attrs 属性来实现指定:

html 复制代码
<!-- MyComponent 模板使用 $attrs 时 -->
<p :class="$attrs.class">Hi!</p>
<span>This is a child component</span>
html 复制代码
<MyComponent class="baz" />

这将被渲染为:

html 复制代码
<p class="baz">Hi!</p>
<span>This is a child component</span>

绑定内联样式style

绑定对象

:style 支持绑定 JavaScript 对象值,对应的是 HTML 元素的 style 属性:

html 复制代码
data() {
  return {
    activeColor: 'red',
    fontSize: 30
  }
}
html 复制代码
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

尽管推荐使用 camelCase,但 :style 也支持 kebab-cased 形式的 CSS 属性 key (对应其 CSS 中的实际名称),例如:

html 复制代码
<div :style="{ 'font-size': fontSize + 'px' }"></div>

直接绑定一个样式对象通常是一个好主意,这样可以使模板更加简洁:

javascript 复制代码
data() {
  return {
    styleObject: {
      color: 'red',
      fontSize: '13px'
    }
  }
}
html 复制代码
<div :style="styleObject"></div>

同样的,如果样式对象需要更复杂的逻辑,也可以使用返回样式对象的计算属性。

绑定数组

我们还可以给 :style 绑定一个包含多个样式对象的数组。这些对象会被合并后渲染到同一元素上:

html 复制代码
<div :style="[baseStyles, overridingStyles]"></div>
相关推荐
To_OC21 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
To_OC21 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
风月说与山鬼1 天前
三、大括号语法
前端·react.js
kyriewen1 天前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
Csvn1 天前
🕰️ 闭包 + setTimeout 的 5 个经典陷阱:为什么定时器看到的永远不是最新的值?
前端
lilian2331 天前
Harmony os 技术实战|拼豆制图27:用单字符编码承载 50 张 70×70 图纸
前端·数据库·华为·harmonyos
CarIise1 天前
CSS选择器与样式关联
前端·css·tensorflow
里欧跑得慢1 天前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
IT_陈寒1 天前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端