国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue.2.0.5實現Class 與 Style 綁定的實例

2019-11-19 16:18:00
字體:
來源:轉載
供稿:網友

Class 與 Style 綁定

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

綁定 HTML Class

對象語法

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

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

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

我們也可以在對象中傳入更多屬性用來動態切換多個 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 列表將相應地更新。例如,如果 hasError的值為 true , class列表將變為 "static active text-danger"。

你也可以直接綁定數據里的一個對象:

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

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

<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 列表:

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

渲染為:

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

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

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

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

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

<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>

綁定內聯樣式

對象語法

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

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

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

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

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

數組語法

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

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

自動添加前綴

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奎屯市| 康定县| 石狮市| 福贡县| 砀山县| 海淀区| 炉霍县| 和平县| 崇信县| 买车| 台安县| 巴塘县| 德惠市| 凌源市| 布拖县| 肇州县| 平谷区| 扎囊县| 夹江县| 东平县| 博客| 栖霞市| 静乐县| 那曲县| 昔阳县| 沂源县| 沙坪坝区| 共和县| 石台县| 奉节县| 洞口县| 娄底市| 弥勒县| 公安县| 洞头县| 武胜县| 滦平县| 都昌县| 南川市| 电白县| 余姚市|