v-bind: 简写可省略不写
如 v-bind:class="" 简写 :class=""
首先创建一个样式文件style.css
body{
font-size:24px;
}
.active{
color:red;
}
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>class绑定</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <link href="../vue_cli/css/style.css" rel="stylesheet"> </head> <body> <div id="myPage"> <div v-bind:class="{active:isActive}">测试文本内容</div> <div :class="{active:isActive}">测试文本内容</div> <button @click="btnclick">class绑定</button> </div> <script> var myPage = new Vue({ el:'#myPage', data:{ isActive:false }, methods:{ btnclick:function() {
//对当前值取反
this.isActive=!this.isActive;
}
}
})
</script>
</body>
</html>
效果图:
点击按钮后,效果展示: