Home > Web Front-end > JS Tutorial > vue.js binds class and style styles

vue.js binds class and style styles

高洛峰
Release: 2017-01-12 13:29:27
Original
1156 people have browsed it

A common requirement for data binding is to manipulate the element's class list and its inline styles. Since they are all attributes, we can use v-bind to handle them: we just need to evaluate the final string of the expression. However, string concatenation is cumbersome and error-prone. Therefore, Vue.js specifically enhances v-bind when used with classes and styles. In addition to strings, the result type of an expression can also be an object or an array.

Use v-bind:class or :class to bind style or class

Bind class

<div class="test">
  <div :class="{active:isActive,cc:isCc}"></div>
 </div>
Copy after login

js code

var myVue = new Vue({
   el: ".test",
   data: {
     isActive:true,
     isCc:false
   },
 });
Copy after login

Or the following code can also be implemented

<div class="test">
   <div :class=classObject></div>
 </div>
Copy after login

js code

var myVue = new Vue({
    el: ".test",
    data: {
      classObject:{
        active:true
      }
    },
  });
Copy after login

pass the class through the array Binding

<div class="test">
  <div :class="[activeClass,error]">dsdsd</div>
</div>
Copy after login

js code

var myVue = new Vue({
   el: ".test",
   data: {
     activeClass:"active",
     error:"ddd"
   },
 });
Copy after login

Bind style attribute

<div class="test">
    <div :style=styleObject>dsdsd</div>
  </div>
Copy after login

js code

var myVue = new Vue({
  el: ".test",
  data: {
    styleObject:{
     color: "red",
      fontSize: "30px"
    }
  },
});
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to vue.js binding classes and styles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template