class 与 style 是 html 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
以下例子,把class样式设置在style标签中,vue实例中只存在一个布尔值isActive ,用v-bind:class=”{ active: isActive }”的方式绑定样式,根据布尔值来决定是否渲染。
<style>.active {
width: 100px;
height: 100px;
background: green;}</style><p>
</p><p></p><script>new Vue({
el: '#app',
data: {
flag: true
}
})
</script>
绑定多个class
v-bind:class="{ active: isActive, 'text-danger': hasError }">
以对象形式绑定多个class
<p>
</p><p></p><script>new Vue({
el: '#app',
data: {
classObject: {
active: true,
'text-danger': true
}
}
})
</script>
-
以数组的方式绑定样式
<p>
</p><p></p><script>new Vue({
el: '#app',
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
})
</script>
内联样式的绑定
<p>
</p><p>菜鸟教程</p><script>new Vue({
el: '#app',
data: {
activeColor: 'green',
fontSize: 30
}
})</script>
用对象方式绑定内联样式
<p>
</p><p>菜鸟教程</p><script>new Vue({
el: '#app',
data: {
styleObject: {
color: 'green',
fontSize: '30px'
}
}
})
</script>
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!











