Vue v-bind 指令

实例

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

  1. <template>
  2. <h2>v-bind 指令</h2>
  3. <p>v-bind 指令将 DIV 元素的样式属性连接到 colorVal 数据属性。</p>
  4. <div v-bind:style="{ backgroundColor: colorVal }">DIV element</div>
  5. <p>Use the input type="color" box below to change the color.</p>
  6. <p><input type="color" v-model="colorVal"> <pre>colorVal: '{{ colorVal }}'</pre></p>
  7. </template>

定义与用法

v-bind 指令用于将 HTML 属性绑定到 Vue 实例中的属性(上面的实例),或传递 props(下面的实例 1)。

如果我们更改 Vue 实例属性,并且该属性通过 v-bind 绑定到 HTML 属性,则由于 Vue 的反应系统,HTML 元素将自动更新为新的属性值。

当与 .prop 修饰符一起使用时,v-bind: 的简写为 :.。这些修饰符可以与 v-bind 指令一起使用,但通常不需要:

修饰符详情
.camel将属性名称从 kebab-case 转换为 camelCase 当使用构建步骤或使用字符串模板时,不需要这样做
.prop强制将绑定设置为 DOM 属性除非使用自定义元素,否则 Vue 会发现 v-bind 提供的键是 DOM 属性还是元素的属性,并适当地绑定该键。
.attr强制将绑定设置为 DOM 属性除非使用自定义元素,否则 Vue 会发现 v-bind 提供的键是 DOM 属性还是元素的属性,并适当地绑定该 key。

更多实例

实例 1

使用 v-bind 发送 'img' prop,数据类型为布尔(true/false

  1. <template>
  2. <h2>v-bind 指令实例</h2>
  3. <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>
  4. <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}
  5. <info-box
  6. turtle-text="Turtles can hold their breath for a long time."
  7. v-bind:turtle-img="img"
  8. />
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. img: true
  15. }
  16. }
  17. }
  18. </script>
实例 2

使用 v-bind: 简写 :

  1. <template>
  2. <h2>v-bind 指令实例</h2>
  3. <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>
  4. <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}
  5. <info-box
  6. turtle-text="Turtles can hold their breath for a long time."
  7. :turtle-img="img"
  8. />
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. img: true
  15. }
  16. }
  17. }
  18. </script>
实例 3

使用 .prop 修饰符更改复选框的 indeterminate DOM 属性。

  1. <template>
  2. <p>Using the '.prop' modifier to toggle the 'indeterminate' appearance of the checkbox:</p>
  3. <button v-on:click="indetVal = !indetVal">Toggle</button>
  4. <p>
  5. <input type="checkbox" v-bind:indeterminate.prop="indetVal"> Checkbox
  6. </p>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. indetVal: false
  13. };
  14. }
  15. };
  16. </script>
  17. <style>
  18. input {
  19. margin: 20px;
  20. scale: 2;
  21. }
  22. </style>
实例 4

使用 .prop 修饰符简写和 v-bind 简写,因此 v-bind:indeterminate.prop 变成 .indeterminate

  1. <template>
  2. <p>使用 the '.prop' 简写因此 'v-bind:indeterminate.prop' 变成 '.indeterminate':</p>
  3. <button v-on:click="indetVal = !indetVal">切换</button>
  4. <p>
  5. <input type="checkbox" .indeterminate="indetVal"> Checkbox
  6. </p>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. indetVal: false
  13. };
  14. }
  15. };
  16. </script>
  17. <style scoped>
  18. input {
  19. margin: 10px;
  20. scale: 2;
  21. }
  22. </style>

分类导航