style.css
body{
font-size:24px;
}
.active{
color:red;
}
.big{
font-size:64px;
}
<!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 :class="mystyle">测试文本内容</div> <button @click="btnclick">class对象绑定</button> </div> <script> var myPage = new Vue({ el:'#myPage', data:{ mystyle:{ active:true, big:true, } }, methods:{ btnclick:function() { //对当前值取反 this.mystyle.big=!this.mystyle.big; } } }) </script> </body> </html>
效果图:
点击按钮后效果: