Vue動(dòng)態(tài)類的幾種使用方法總結(jié)
Vue動(dòng)態(tài)類的幾種使用
動(dòng)態(tài)類的操作比較簡單,但是很實(shí)用。
點(diǎn)擊顯示或隱藏樣式的做法
利用帶數(shù)據(jù)綁定
<button @click="isShow = !isShow">點(diǎn)擊</button>
<div :class="{ colorRed: isShow }">哈哈哈</div>data() {
? return {
? isShow: true
}.colorRed {
?? ?color:red;
}
.colorBlue{
?? ?color:blue;
}代碼含義:當(dāng)isShow 為true時(shí),添加類名colorRed ,div字體為紅色,渲染結(jié)果如下:
<div class="colorRed ">哈哈哈</div>
利用三元表達(dá)式切換樣式
控制isShow 切換樣式
<div :class="[isShow ?'colorRed':'colorBlue']">哈哈哈</div>
多個(gè)動(dòng)態(tài)類的用法
案例 : 用帶有圖標(biāo)的按鈕展開列表和收起列表

<i :class="{'el-icon-d-arrow-left':!menuIsShow,'el-icon-d-arrow-right': menuIsShow}"
class="el-icon-d-arrow-left openMenu"
@click="menuIsShow=!menuIsShow">
</i>
el-icon-d-arrow-left 是 element自帶的字體圖標(biāo)
data() {
return {
menuIsShow: false
};
},
小結(jié):利用動(dòng)態(tài)類可以節(jié)省很多代碼,實(shí)現(xiàn)更復(fù)雜的功能。
Vue class動(dòng)態(tài)類名
1. 三元表達(dá)式
//1. html
<view :class="`stage-${item.state == '通過' ? 'pass' : 'nopass'}`"></view>
?
//2.style
.stage-pass {
? ? background: green;
}
.stage-nopass {
? ? background: red;
}2. 對象的形式:可以寫多個(gè),用逗號分開(常用)。
<div class="steps-list-cont" :class="{ stage: item.label == '繳納成功', 'stage-pass': true ?}"> </div>3.傳方法,在方法中返回類名,這個(gè)方法是可以觸發(fā)的哦~,或者通過計(jì)算屬性也是妥妥滴
// 使用方法?
<view :class="queryBoxClassNames()"></view>
?
methods: {
? ? // 動(dòng)態(tài)獲取類名
? ? queryBoxClassNames() {
? ? ? ? const classNames = {
? ? ? ? ? ? JMJS: ['fixed-top', 'fixed-right']
? ? ? ? }
? ? ? ? return classNames[this.type] || ['fixed-bottom', 'fixed-left'];
? ? },
}
?
----------------------------------
?
// 使用計(jì)算屬性
<view :class="queryBoxClassNames"></view>
?
computed: {
? ? // 動(dòng)態(tài)獲取類名
?? ?queryBoxClassNames() {
? ? ? ? const classNames = {
? ? ? ? ? ? JMJS: ['fixed-top', 'fixed-right']
? ? ? ? }
? ? ? ? return classNames[this.type] || ['fixed-bottom', 'fixed-left'];
? ? },相當(dāng)于
<view v-if="fType !== 'JMJS'" class="fixed-bottom fixed-left"></view> <view v-if="fType == 'JMJS'" class="fixed-top fixed-right"></view>
4.根據(jù)計(jì)算屬性判斷添加類名,該寫法多用于被封裝的組件庫內(nèi)
?<template>
? <transition name="mint-toast-pop">
? ? <div class="mint-toast" v-show="visible" :class="customClass" :style="{ 'padding': iconClass === '' ? '10px' : '20px' }"></div>
? </transition>
</template>
?
?
computed: {
? ? ? customClass() {
? ? ? ? var classes = [];
? ? ? ? switch (this.position) {
? ? ? ? ? case 'top':
? ? ? ? ? ? classes.push('is-placetop');
? ? ? ? ? ? break;
? ? ? ? ? case 'bottom':
? ? ? ? ? ? classes.push('is-placebottom');
? ? ? ? ? ? break;
? ? ? ? ? default:
? ? ? ? ? ? classes.push('is-placemiddle');
? ? ? ? }
? ? ? ? classes.push(this.className);
?
? ? ? ? return classes.join(' ');
? ? ? }
? ? }總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)將時(shí)間戳轉(zhuǎn)換成日期格式
這篇文章主要介紹了vue實(shí)現(xiàn)將時(shí)間戳轉(zhuǎn)換成日期格式方式,具有很好的參考價(jià)值,希望對大家有所幫助,2023-10-10
淺析Proxy如何實(shí)現(xiàn)Vue響應(yīng)式
這篇文章主要是來和大家探討一下,Vue的響應(yīng)式系統(tǒng)僅僅是一個(gè)Proxy嗎,本文將圍繞此問題探索一下Proxy是如何實(shí)現(xiàn)Vue響應(yīng)式的,感興趣的小伙伴可以了解一下2023-08-08
vue使用Sass時(shí)報(bào)錯(cuò)問題的解決方法
這篇文章主要介紹了vue使用Sass時(shí)報(bào)錯(cuò)問題的解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀
這篇文章主要介紹了Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
vue 實(shí)現(xiàn)websocket發(fā)送消息并實(shí)時(shí)接收消息
這篇文章主要介紹了vue 實(shí)現(xiàn)websocket發(fā)送消息并實(shí)時(shí)接收消息,項(xiàng)目結(jié)合vue腳手架和websocket來搭建,本文給大家分享實(shí)例代碼,需要的朋友可以參考下2019-12-12
說說Vue.js中的functional函數(shù)化組件的使用
這篇文章主要介紹了說說Vue.js中的functional函數(shù)化組件的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
vue如何統(tǒng)一樣式(reset.css與border.css)
這篇文章主要介紹了vue如何統(tǒng)一樣式(reset.css與border.css),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Nuxt 項(xiàng)目性能優(yōu)化調(diào)研分析
這篇文章主要介紹了Nuxt 項(xiàng)目性能優(yōu)化調(diào)研分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

