Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法
項(xiàng)目開發(fā)中給元素添加/刪除 class 是非常常見的行為之一, 例如網(wǎng)站導(dǎo)航都會(huì)給選中項(xiàng)添加一個(gè) active 類用來區(qū)別選與未選中的樣式,除了導(dǎo)航之外其他很多地方也都會(huì)用到這種方式處理選中與未選中。
除了設(shè)置 class 我們?cè)陧?xiàng)目中也經(jīng)常設(shè)置元素的內(nèi)聯(lián)樣式 style,在 jquery 時(shí)代我們大多數(shù)都是利用 addClass 與 removeClass 結(jié)合使用來處理 class 的添加/刪除,利用 css() 方法設(shè)置/獲取元素的內(nèi)聯(lián)樣式。
那么在 vue 中 我們?nèi)绾翁幚磉@類的效果呢?在 vue 中我們可以利用 v-bind 指令綁定我們的 class 與 style,接下來我們看看 vue 中給我們提供了哪些綁定它們的方式。
對(duì)象語法綁定 Class
Tab 頁的切換是我們最常見的一個(gè)效果之一,如何讓選中的標(biāo)題高亮,常用的方式就是動(dòng)態(tài)切換 class 。

<div id="app">
<div class="button-group">
<button
v-for="(tab, index) in tabs"
v-bind:key="index"
v-bind:class="{active: currentTab === tab}"
v-on:click="currentTab = tab"
>{{tab}}</button>
</div>
<component v-bind:is="currentTabComponent"></component>
</div>
<script>
Vue.component("tab1", {
"template": "<p>這里是標(biāo)簽頁1</p>"
});
Vue.component("tab2", {
"template": "<p>這里是標(biāo)簽頁2</p>"
});
Vue.component("tab3", {
"template": "<p>這里是標(biāo)簽頁3</p>"
});
var vm = new Vue({
el: "#app",
data: {
currentTab: "tab1",
tabs: ["tab1", "tab2", "tab3"]
},
computed: {
currentTabComponent() {
return this.currentTab;
}
}
});
</script>
從例子中我們看到 active 這個(gè) class 是否存在取決于后面的表達(dá)式是真值或者假值,當(dāng)為真值時(shí) active 類被添加到元素上否則沒有。
我們不僅可以添加一個(gè) class,我們還可以同時(shí)添加多個(gè) class,并且還可以與原有的 class 共存。
<button
class="btn"
v-bind:class="{'btn-primary': isPrimary, active: isActive}"
></button>
<script>
var vm = new Vue({
el: "#app",
data: {
isPrimary: true,
isActive: true
}
});
</script>
渲染結(jié)果為:
<button class="btn btn-primary active"></button>
我們也可以直接綁定一個(gè)數(shù)據(jù)對(duì)象
<button class="btn" v-bind:class="activePrimary"></button>
<script>
var vm = new Vue({
el: "#app",
data: {
activePrimary: {
'btn-primary': true,
active: true
}
}
});
</script>
渲染結(jié)果與上面相同
<button class="btn btn-primary active"></button>
除此之外,我們還可以使用計(jì)算屬性去綁定元素的 class
<button v-bind:class="activeClass"></button>
<script>
var vm = new Vue({
el: "#app",
data: {
isActive: true
},
computed: {
activeClass() {
return {
active: this.isActive
}
}
}
});
</script>
數(shù)組語法綁定 Class
Vue 中還支持我們給元素利用數(shù)組的方式添加 class,我們修改上面對(duì)象添加 class 的例子。
<button class="btn" v-bind:class="[primary, active]"></button>
<script>
var vm = new Vue({
el: "#app",
data: {
primary: 'btn-primary',
active: 'btn-active'
}
});
</script>
上面方式我們綁定了固定不變的,如果我們需要?jiǎng)討B(tài)的切換 class 怎么辦呢? 我們可以利用三元表達(dá)式或者在數(shù)組中使用對(duì)象語法。
//三元表達(dá)式
<button v-bind:class="[isActive ? active : '', primary]"></button>
<script>
var vm = new Vue({
el: "#app",
data: {
isActive: true,
primary: 'btn-primary',
active: 'btn-active'
}
});
</script>
//數(shù)組中使用對(duì)象語法
<button v-bind:class="[{active: isActive}, primary]"></button>
<script>
var vm = new Vue({
el: "#app",
data: {
isActive: true,
primary: 'btn-primary'
}
});
</script>
對(duì)象語法綁定 Style
綁定內(nèi)聯(lián)樣式時(shí)的對(duì)象語法,看起來非常像 css,但他其實(shí)是一個(gè) Javascript 對(duì)象,我們可以使用駝峰式或者短橫線分隔命名。
<div v-bind:style="{color: colorStyle, backgroundColor: background}">
對(duì)象語法
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
colorStyle: 'red',
background: 'blue'
}
});
</script>
與 class 類似我們也可以使用數(shù)據(jù)對(duì)象的方式綁定。
<div v-bind:style="style">對(duì)象語法</div>
<script>
var vm = new Vue({
el: "#app",
data: {
style: {
color: 'red',
backgroundColor: 'blue'
}
}
});
</script>
數(shù)組語法綁定 Style
Vue 允許我們同時(shí)綁定多個(gè)樣式對(duì)象作用于同一個(gè)對(duì)象上。
<div v-bind:style="[style, fontStyle]">對(duì)象語法</div>
<script>
var vm = new Vue({
el: "#app",
data: {
style: {
color: 'red',
backgroundColor: 'blue'
},
fontStyle: {
fontSize: '18px'
}
}
});
</script>
到此這篇關(guān)于Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法的文章就介紹到這了,更多相關(guān)Vue v-bind綁定用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口
這篇文章主要介紹了vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
vue實(shí)現(xiàn)全局組件自動(dòng)注冊(cè),無需再單獨(dú)引用
這篇文章主要介紹了vue實(shí)現(xiàn)全局組件自動(dòng)注冊(cè),無需再單獨(dú)引用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
VUE3刷新頁面報(bào)錯(cuò)問題解決:Uncaught?SyntaxError:Unexpected?token?&apo
這篇文章主要介紹了VUE3刷新頁面報(bào)錯(cuò):Uncaught?SyntaxError:?Unexpected?token?‘<‘,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
vue項(xiàng)目中created()被調(diào)用多次的踩坑實(shí)戰(zhàn)
在vue項(xiàng)目中我在created中調(diào)用了兩次get數(shù)據(jù)請(qǐng)求,所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中created()被調(diào)用多次的踩坑實(shí)戰(zhàn),需要的朋友可以參考下2023-03-03
vue項(xiàng)目中在可編輯div光標(biāo)位置插入內(nèi)容的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue項(xiàng)目中在可編輯div光標(biāo)位置插入內(nèi)容的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

