vue10-class陣列樣式繫結
阿新 • • 發佈:2018-12-11
首先看程式碼
<!DOCTYPE html> <html lang=en> <head> <meta charset="utf-8" /> <title>Hello world</title> <script src="vue.js"></script> <style> .activated { color: red; } .activated-one { font-size: 50px; } </style> </head> <body> <div id="root"> <!--class陣列繫結 後面加一個數組,可以寫多個值,陣列的值會被vue中的值代替 --> <div @click="handleDivClick" :class="[activated,activatedOne]">HelloWorld</div> </div> <script> var vm = new Vue({ el: "#root", data: { activated: "", activatedOne: "activated-one" }, methods: { handleDivClick: function () { if (this.activated === "activated") { this.activated = ""; } else { this.activated = "activated"; } } } }) </script> </body> </html>
vue繫結class屬性,後面跟著要給陣列,陣列的值就是vue例項中變數的值,把樣式的名稱賦值給變數,渲染後就賦給了class屬性。