選單
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首頁 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue 為什麼、如何和設定 Vue 第一個 SFC 頁面 Vue 元件 Vue Props Vue v-for 元件 Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue 本地元件 Vue Slots Vue v-slot Vue Scoped Slots Vue 動態元件 Vue Teleport Vue HTTP 請求 Vue Template Refs Vue 生命週期鉤子 Vue Provide/Inject Vue 路由 Vue 表單輸入 Vue 動畫 Vue 結合 v-for 的動畫 Vue 構建 Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue v-bind 指令


示例

使用 v-bind 指令更改 <div> 元素的背景顏色。

<template>
  <h2>Example v-bind Directive</h2>
  <p>The v-bind directive connects the style attribute of the DIV element to the 'colorVal' data property.</p>
  <div v-bind:style="{ backgroundColor: colorVal }">DIV element</div>
  <p>Use the input type="color" box below to change the color.</p>
  <p><input type="color" v-model="colorVal"> <pre>colorVal: '{{ colorVal }}'</pre></p>
</template>
執行示例 »

更多示例請參見下方。


定義和用法

v-bind 指令用於將 HTML 屬性繫結到 Vue 例項中的屬性(如上例),或傳遞 props(如下例 1)。

如果我們更改 Vue 例項的屬性,並且該屬性已透過 v-bind 繫結到 HTML 屬性,則由於 Vue 的響應式系統,HTML 元素將自動更新為新的屬性值。

v-bind:”的簡寫形式是“:”,或者當與 .prop 修飾符一起使用時是“.”。

這些修飾符可以與 v-bind 指令一起使用,但通常不需要

修飾符 詳情
.camel 將屬性名稱從 kebab-case 轉換為 camelCase。使用構建步驟或字串模板時不需要此選項。
.prop 強制將繫結設定為 DOM 屬性。除非處理自定義元素,否則 Vue 會找出 v-bind 提供的鍵是 DOM 屬性還是元素的屬性,並相應地進行繫結。
.attr 強制將繫結設定為 DOM 屬性。除非處理自定義元素,否則 Vue 會找出 v-bind 提供的鍵是 DOM 屬性還是元素的屬性,並相應地進行繫結。

更多示例

示例 1

使用 v-bind 傳送 'img' prop,資料型別為布林值(true/false)。

<template>
  <h2>Example v-bind Directive</h2>
  <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>
  <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}
  <info-box 
    turtle-text="Turtles can hold their breath for a long time."
    v-bind:turtle-img="img"
  />
</template>

<script>
export default {
  data() {
    return {
      img: true
    }
  }
}
</script>
執行示例 »

示例 2

使用“v-bind:”的簡寫“:”。

<template>
  <h2>Example v-bind Directive</h2>
  <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>
  <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}
  <info-box 
    turtle-text="Turtles can hold their breath for a long time."
    :turtle-img="img"
  />
</template>

<script>
export default {
  data() {
    return {
      img: true
    }
  }
}
</script>
執行示例 »

示例 3

使用 .prop 修飾符更改複選框的 indeterminate DOM 屬性。

<template>
  <p>Using the '.prop' modifier to toggle the 'indeterminate' appearance of the checkbox:</p>
  <button v-on:click="indetVal = !indetVal">Toggle</button>
  <p>
    <input type="checkbox" v-bind:indeterminate.prop="indetVal"> Checkbox
  </p>
</template>

<script>
export default {
  data() {
    return {
      indetVal: false
    };
  }
};
</script>

<style>
input {
  margin: 20px;
  scale: 2;
}
</style>
執行示例 »

示例 4

使用 .prop 修飾符的簡寫,以及 v-bind 的簡寫,因此“v-bind:indeterminate.prop”變為“.indeterminate”。

<template>
  <p>Using the '.prop' shorthand so that 'v-bind:indeterminate.prop' becomes '.indeterminate':</p>
  <button v-on:click="indetVal = !indetVal">Toggle</button>
  <p>
    <input type="checkbox" .indeterminate="indetVal"> Checkbox
  </p>
</template>

<script>
export default {
  data() {
    return {
      indetVal: false
    };
  }
};
</script>

<style scoped>
input {
  margin: 10px;
  scale: 2;
}
</style>
執行示例 »

相關頁面

Vue 教程:Vue CSS 繫結

Vue 教程:Vue v-bind 指令

Vue 教程:Vue Props


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援