詳解VueJs中的V-bind指令

引子

v-bind  主要用於屬性綁定,Vue官方提供了一個簡寫方式 :bind,例如:

?

1

2

3

4

<!-- 完整語法 -->

<a v-bind:href="url"></a>

<!-- 縮寫 -->

<a :href="url"></a>

  一、概述

      v-bind  主要用於屬性綁定,比方你的class屬性,style屬性,value屬性,href屬性等等,只要是屬性,就可以用v-bind指令進行綁定。

      示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<!-- 綁定一個屬性 -->

<img v-bind:src="imageSrc">

<!-- 縮寫 -->

<img :src="imageSrc">

<!-- 內聯字符串拼接 -->

<img :src="'/path/to/images/' + fileName">

<!-- class 綁定 -->

<div :class="{ red: isRed }"></div>

<div :class="[classA, classB]"></div>

<div :class="[classA, { classB: isB, classC: isC }]">

<!-- style 綁定 -->

<div :style="{ fontSize: size + 'px' }"></div>

<div :style="[styleObjectA, styleObjectB]"></div>

<!-- 綁定一個有屬性的對象 -->

<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>

<!-- 通過 prop 修飾符綁定 DOM 屬性 -->

<div v-bind:text-content.prop="text"></div>

<!-- prop 綁定。“prop”必須在 my-component 中聲明。-->

<my-component :prop="someThing"></my-component>

<!-- 通過 $props 將父組件的 props 一起傳給子組件 -->

<child-component v-bind="$props"></child-component>

<!-- XLink -->

<svg><a :xlink:special="foo"></a></svg>

二、綁定 HTML Class

對象語法

       我們可以傳給 v-bind:class 一個對象,以動態地切換 class

?

1

<div v-bind:class="{ active: isActive }"></div>

 

      上面的語法表示 active 這個 class 存在與否將取決於數據屬性 isActive 的 truthiness

你可以在對象中傳入更多屬性來動態切換多個 class。此外,v-bind:class 指令也可以與普通的 class 屬性共存。當有如下模板:

?

1

2

3

4

5

6

7

8

9

<div class="static"

  v-bind:class="{ active: isActive, 'text-danger': hasError }">

</div>

  和如下 data

 

data: {

 isActive: true,

 hasError: false

}

       結果渲染爲:

?

1

<div class="static active"></div>

    當 isActive 或者 hasError 變化時,class 列表將相應地更新。例如,如果 hasError 的值爲 true,class 列表將變爲 "static active text-danger"

      綁定的數據對象不必內聯定義在模板裏

?

1

2

3

4

5

6

7

<div v-bind:class="classObject"></div>

data: {

 classObject: {

 active: true,

 'text-danger': false

 }

}

       渲染的結果和上面一樣。我們也可以在這裏綁定一個返回對象的計算屬性。這是一個常用且強大的模式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<div v-bind:class="classObject"></div>

 

data: {

 isActive: true,

 error: null

},

computed: {

 classObject: function () {

 return {

  active: this.isActive && !this.error,

  'text-danger': this.error && this.error.type === 'fatal'

 }

 }

}

數組語法

    我們可以把一個數組傳給 v-bind:class,以應用一個 class 列表

?

1

2

3

4

5

<div v-bind:class="[activeClass, errorClass]"></div>

data: {

 activeClass: 'active',

 errorClass: 'text-danger'

}

   渲染爲:

?

1

<div class="active text-danger"></div>

     如果你也想根據條件切換列表中的 class,可以用三元表達式

?

1

<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>

    這樣寫將始終添加 errorClass,但是只有在 isActive 是 truthy 時才添加 activeClass。

     不過,當有多個條件 class 時這樣寫有些繁瑣。所以在數組語法中也可以使用對象語法

?

1

<div v-bind:class="[{ active: isActive }, errorClass]"></div>

三、用在組件上

        當在一個自定義組件上使用 class 屬性時,這些類將被添加到該組件的根元素上面。這個元素上已經存在的類不會被覆蓋。

       例如,如果你聲明瞭這個組件: 

?

1

2

3

Vue.component('my-component', {

 template: '<p class="foo bar">Hi</p>'

})

     然後在使用它的時候添加一些 class

?

1

<my-component class="baz boo"></my-component>

     HTML 將被渲染爲:

?

1

<p class="foo bar baz boo">Hi</p>

     對於帶數據綁定 class 也同樣適用   

?

1

<my-component v-bind:class="{ active: isActive }"></my-component>

   當 isActive 爲 truthy時,HTML 將被渲染成爲

?

1

<p class="foo bar active">Hi</p>

 四、綁定內聯樣式

 對象語法

  v-bind:style 的對象語法十分直觀——看着非常像 CSS,但其實是一個 JavaScript 對象。CSS 屬性名可以用駝峯式 (camelCase) 或短橫線分隔 (kebab-case,記得用單引號括起來) 來命名: 

?

1

2

3

4

5

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

data: {

 activeColor: 'red',

 fontSize: 30

}

     直接綁定到一個樣式對象通常更好,這會讓模板更清晰

?

1

2

3

4

5

6

7

8

<div v-bind:style="styleObject"></div>

 

data: {

 styleObject: {

 color: 'red',

 fontSize: '13px'

 }

}

   同樣的,對象語法常常結合返回對象的計算屬性使用

    數組語法

   v-bind:style 的數組語法可以將多個樣式對象應用到同一個元素上

?

1

<div v-bind:style="[baseStyles, overridingStyles]"></div>

總結

以上所述是小編給大家介紹的VueJs中的V-bind指令,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對腳本之家網站的支持!

發佈了57 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章