内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Vue.2.0.5如何實現(xiàn)Class與Style綁定的實例

小編給大家分享一下Vue.2.0.5如何實現(xiàn)Class與Style綁定的實例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

團風網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司從2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

Class 與 Style 綁定

數(shù)據(jù)綁定一個常見需求是操作元素的 class 列表和它的內(nèi)聯(lián)樣式。因為它們都是屬性 ,我們可以用v-bind 處理它們:只需要計算出表達式最終的字符串。不過,字符串拼接麻煩又易錯。因此,在 v-bind 用于 class 和 style 時, Vue.js 專門增強了它。表達式的結(jié)果類型除了字符串之外,還可以是對象或數(shù)組。

綁定 HTML Class

對象語法

我們可以傳給 v-bind:class 一個對象,以動態(tài)地切換 class 。

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

上面的語法表示 classactive 的更新將取決于數(shù)據(jù)屬性 isActive 是否為真值 。

我們也可以在對象中傳入更多屬性用來動態(tài)切換多個 class 。此外, v-bind:class 指令可以與普通的 class 屬性共存。如下模板:

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

如下 data:

data: {
 isActive: true,
 hasError: false
}

渲染為:

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

當 isActive 或者 hasError 變化時,class 列表將相應(yīng)地更新。例如,如果 hasError的值為 true , class列表將變?yōu)?"static active text-danger"。

你也可以直接綁定數(shù)據(jù)里的一個對象:

<div v-bind:class="classObject"></div>
data: {
 classObject: {
  active: true,
  'text-danger': false
 }
}

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

<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',
  }
 }
}

數(shù)組語法

我們可以把一個數(shù)組傳給 v-bind:class ,以應(yīng)用一個 class 列表:

<div v-bind:class="[activeClass, errorClass]">
data: {
 activeClass: 'active',
 errorClass: 'text-danger'
}

渲染為:

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

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

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

此例始終添加 errorClass ,但是只有在 isActive 是 true 時添加 activeClass 。

不過,當有多個條件 class 時這樣寫有些繁瑣??梢栽跀?shù)組語法中使用對象語法:

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

With Components

This section assumes knowledge ofVue Components. Feel free to skip it and come back later.

When you use the class attribute on a custom component, those classes will be added to the component's root element. Existing classes on this element will not be overwritten.

For example, if you declare this component:

Vue.component('my-component', {
 template: '<p class="foo bar">Hi</p>'
})

Then add some classes when using it:

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

The rendered HTML will be:

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

The same is true for class bindings:

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

When isActive is truthy, the rendered HTML will be:

<div class="foo bar active"></div>

綁定內(nèi)聯(lián)樣式

對象語法

v-bind:style 的對象語法十分直觀——看著非常像 CSS ,其實它是一個 JavaScript 對象。 CSS 屬性名可以用駝峰式(camelCase)或短橫分隔命名(kebab-case):

<div v-bind:></div>
data: {
 activeColor: 'red',
 fontSize: 30
}

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

<div v-bind:></div>
data: {
 styleObject: {
  color: 'red',
  fontSize: '13px'
 }
}

同樣的,對象語法常常結(jié)合返回對象的計算屬性使用。

數(shù)組語法

v-bind:style 的數(shù)組語法可以將多個樣式對象應(yīng)用到一個元素上:

<div v-bind:>

自動添加前綴

當 v-bind:style 使用需要特定前綴的 CSS 屬性時,如 transform ,Vue.js 會自動偵測并添加相應(yīng)的前綴。

以上是“Vue.2.0.5如何實現(xiàn)Class與Style綁定的實例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標題:Vue.2.0.5如何實現(xiàn)Class與Style綁定的實例
當前網(wǎng)址:http://www.rwnh.cn/article8/psgeip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站定制網(wǎng)站、手機網(wǎng)站建設(shè)電子商務(wù)、域名注冊、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
偃师市| 舟曲县| 远安县| 临沭县| 盐亭县| 且末县| 托克逊县| 阿克苏市| 文安县| 依兰县| 长岭县| 依兰县| 启东市| 上杭县| 调兵山市| 贡山| 泗洪县| 读书| 额敏县| 图木舒克市| 凤城市| 新丰县| 昌邑市| 滨海县| 手游| 外汇| 德安县| 马边| 中牟县| 鄂州市| 时尚| 福安市| 蒙山县| 洪泽县| 惠水县| 平乡县| 山丹县| 自贡市| 绍兴市| 临邑县| 保靖县|