文章目录
1、在对象使用
1.1、绑定单个属性
需求说明
绑定class属性之后的div渲染结果如何
源码实现
1、创建vue项目
vue create 项目名字
2、代码实现
<template>
<div id="app">
<router-link to="/home">首页</router-link>
<router-link to="/about">About</router-link>
<div :class="{ active: isActive }">
"<div class='active'>这是渲染后的标签</div>"
</div>
<router-view />
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {
isActive: true,
};
},
};
</script>
3、效果图
1.2、绑定多个属性
简要说明
对象中也可以传入多个属性,来动态切换class。另外,:class可以与普通class共存
需求说明
多个属性共同绑定实现,并且和普通class共同处理,渲染之后的class多个
源码实现
<template>
<div id="app">
<router-link to="/home">首页</router-link>
<router-link to="/about">About</router-link>
<div class="temp" :class="{
'active':isActive,'tActive':isTActive
}"></div>
<router-view />
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {
isActive: true,
tActive:true
};
},
};
</script>
效果图
1.3、绑定表达式过长或逻辑复杂时,还可以绑定一个计算属性
简要说明
当:class的表达式过长或逻辑复杂时,还可以绑定一个计算属性,这是一种友好和常见的用法,一般当条件多于两个时,都可以使用data或者computed。
源码实现
1、创建vue项目
2、源码实现
<template>
<div id="app">
<div :class="classObject">classObject</div>
</div>
</template>
<script>
export default {
name: "App",
components: {
},
data() {
return {
isActived: true,
err: null,
};
},
computed: {
classObject: function () {
return {
active: this.isActived && !this.err,
test: this.err && this.err.type === "fatal",
};
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
3、效果图
4、渲染后的结果
<div class= active" >classobject</div>
2、在数组使用
2.1、当需要应用多个class时,可以使用数组语法,给:class绑定一个数组,应用一个class列表
关键源码实现
<template>
<div id="app">
<div :class="classObject">classObject</div>
<div :class="[isActivate1,isActivate2]">classArray</div>
</div>
</template>
<script>
export default {
name: "App",
components: {
},
data() {
return {
isActived: true,
err: null,
isActivate2:'isActivate2',
isActivate1:'isActivate1',
};
},
computed: {
classObject: function () {
return {
active: this.isActived && !this.err,
test: this.err && this.err.type === "fatal",
};
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
效果图
2.2、三元运算符应用
源码实现
<template>
<div id="app">
<div :class="[isActivated ? isActivate1 : ' ', isActivate2]">三元表达式</div>
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {
isActived: true,
err: null,
isActivate2: "isActivate2",
isActivate1: "isActivate1",
};
},
computed: {
classObject: function () {
return {
active: this.isActived && !this.err,
test: this.err && this.err.type === "fatal",
};
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
效果图
渲染结果
<div class="isActivate2">三元表达式</div>
2.3、class有多个条件时,这样写较为烦琐,可以在数组语法中使用对象语法
关键代码实现
<template>
<div id="app">
<div :class="classObject">classObject</div>
<div :class="[isActivate1, isActivate2]">classArray</div>
<div :class="[isActivated ? isActivate1 : ' ', isActivate2]">三元表达式</div>
<div :class="[{'active':isActivated},isActivate1]"></div>
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {
isActived: true,
err: null,
isActivate2: "isActivate2",
isActivate1: "isActivate1",
};
},
computed: {
classObject: function () {
return {
active: this.isActived && !this.err,
test: this.err && this.err.type === "fatal",
};
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>