Class

  1. <head>
  2. <style>
  3. .page {
  4. height: 200px;
  5. width: 200px;
  6. }
  7. .active {
  8. background: skyblue
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="app">
  14. <!-- 通过对象形式判断 -->
  15. <div class="page" :class="{active:isTrue}"></div>
  16. <!-- 直接放置对象 -->
  17. <div class="page" :class=objActive></div>
  18. <!-- 放置数组 -->
  19. <div class="page" :class=arryActive></div>
  20. <!-- 数组对象混合 -->
  21. <div class="page" :class=objAndArray></div>
  22. </div>
  23. <script type="text/javascript">
  24. let app = new Vue({
  25. el: '#app',
  26. data: {
  27. isTrue: true,
  28. objActive: {
  29. "active": true,
  30. "color": true //"-"非法必须使用字符串
  31. },
  32. arryActive: ['active', 'color'],
  33. objAndArray: ['active', { abc: true}]
  34. }
  35. });
  36. </script>
  37. </body>

Style

<body>
    <div id="app">
         <!-- 通过对象+变量拼接 -->
        <div :style="{background:'red',margin:marginWidth+'px'}" style=" width:100px;height: 100px;"></div>
        <!-- 放置对象 -->
        <div :style="styleObj" style=" width:100px;height: 100px;"></div>
        <!-- 放置数组 -->
        <div :style="styleArry" style=" width:100px;height: 100px;"></div>
    </div>

    <script type="text/javascript">
        let app = new Vue({
            el: '#app',
            data: {                
                marginWidth: 10,
                styleObj: {
                    'background-color': 'blue',
                    height: '200px'
                },
                styleArry: [{
                    'background-color': 'blue',
                    height: '200px'
                }, {
                    'margin-top': '30px'
                }]
            }
        });
    </script>
</body>

动态添加style|class

<div
     class="item"
     v-for="(c1, index) in categoryList"
     :key="c1.categoryId"
     >
  <h3
      @mouseenter="changeIndex(index)"
      :class="{ cur: currentIndex === index }"
      >