全部学科
NodeJS全栈
nodejs
Python全栈
python
📅 2026-05-20 6 分钟 ✍️ juanwangdev

v-bind绑定

v-bind 指令用于响应式地更新 HTML attribute。

基本用法

HTML
<button v-bind:disabled="isDisabled">点击</button>

<!-- 简写 -->
<button :disabled="isDisabled">点击</button>

绑定 class

对象语法

HTML
<div :class="{ active: isActive, 'text-danger': hasError }"></div>

对应数据:

JavaScript
data() {
  return {
    isActive: true,
    hasError: false
  }
}

数组语法

HTML
<div :class="[activeClass, errorClass]"></div>

绑定 style

对象语法

HTML
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

数组语法

HTML
<div :style="[baseStyles, overridingStyles]"></div>

绑定布尔值 Attribute

HTML
<button :disabled="isDisabled">按钮</button>

isDisabled 为 falsy 值时,attribute 会被移除。

要点总结

  • v-bind 简写为 :,用于动态绑定 attribute
  • class 绑定支持对象语法和数组语法
  • style 绑定同样支持对象和数组形式
  • 布尔值 attribute 根据值的真假自动添加或移除
想在手机上练习这篇文章的配套题目?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码
← 上一篇 数据绑定
下一篇 → v-model双向绑定
扫码体验小程序
加载中
想在手机上刷题学习?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码