v-bind动态绑定class

【对象语法】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .active{
            color: red;
        }
    </style>
</head>
<body>
<div id="app">
    <h2 class="title" :class="{active: isActive, line:isLine}">{{message}}</h2>
    <button @click="btnClick">按钮</button>
</div>

<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
      el:'#app',
      data:{
          message: '你好!',
          isActive: true,
          isLine: true
      },
      methods:{
          btnClick: function () {
              this.isActive = !this.isActive;
          }
      }
    })
</script>
</body>
</html>

  

【数组语法】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .active{
            color: red;
        }
    </style>
</head>
<body>
<!--数组语法-->
<div id="app">
    <h2 class="title" :class="['active','line']">{{message}}</h2>
    <h2 class="title" :class="getClasses()">{{message}}</h2>
    <button @click="btnClick()">按钮</button>
</div>

<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
      el:'#app',
      data:{
          message: '你好!',
          active: 'aaa',
          line: 'bbb'
      },
      methods:{
          btnClick: function () {
              this.isActive = !this.isActive;
          },
          getClasses: function () {
              return [this.active,this.line];
          }
      }
    })
</script>
</body>
</html>

  

posted @ 2021-08-07 18:02  Mr_sven  阅读(30)  评论(0编辑  收藏  举报