Detailed explanation of how v-bind dynamically binds class attributes
How does v-bind dynamically bind class attributes? This article will give you a detailed understanding of the various syntaxes of the v-bind instruction to dynamically bind class attributes. I hope it will be helpful to you!
#v-bind can dynamically set the class attribute to implement dynamic styles. Writing method:
<!--完整写法--> <标签名 v-bind:class="vue实例中的数据属性名"/> <!--简化写法--> <标签名 :class="vue实例中的数据属性名"/>
1. v-bind dynamically binds the class attribute (object syntax)
After dynamically binding the class attribute, the class The value is a variable that can be placed in data to dynamically bind the style to dynamically switch classes. (Learning video sharing: vue video tutorial)
1. Directly bind one or more classes through {}
v-blid:class
can pass in an object, which includes a set of key-value pairs
:class= "{key1:value1,key2:value2...}"
The class name is the corresponding style, that is, the key ); value corresponds to adding and removing the class, the values are true
and false
if value is
true
, then the key style will workIf the value value is
false
, then the key style will not work
<!-- 样式 --> <style> .color { color: red; } .background { background-color: pink; } </style>
<div id="app"> <h2 id="message">{{message}}</h2> <!-- 第一种:直接用style中的样式,固定写死; --> <h2 id="message">{{message}}</h2> <!-- 第二种:用指令v-bind,class的属性值color为变量; --> <!-- vue解析,变量对应data中color属性值,则用了color的样式; --> <h2 id="message">{{message}}</h2> <!-- 第三种:用指令v-bind;后面接对象{key1:value1,key2:value2} --> <!-- isColor为false则color样式不起作用。 --> <!-- isBackground为true则background样式起作用。 --> </div> <script> const app = new Vue({ el:"#app", data:{ message:"你好啊", color:"color", isColor:false, isBackground:true } }) </script>
v-bind:class
directive can also coexist with the ordinary class attribute.
<div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }" ></div>
data: { isActive: true, hasError: true }
When isActive
or hasError
changes, the class list will be updated accordingly. For example, if hasError
has the value false
, the class list will become "static active
".
data: { isActive: true, hasError: false }
2. The bound data object does not need to be defined inline in the template, but can be bound to a class object classObject
<!-- 样式 --> <style> .color { color: red; } .background { background-color: pink; } </style>
<div id="app"> <h2 id="message">{{message}}</h2> <!-- 如下:绑定到一个类对象classObject,对象中设置多个key:value对 --> <!-- color为true则color样式起作用。 --> <!-- background为false则background样式不起作用。 --> </div> <script> const app = new Vue({ el:"#app", data:{ message:"你好啊", classObject: { color: true, background: false } } }) </script>
3. You can also bind a calculated property of the returned object
<div id="app"> <h2 id="message">{{message}}</h2> <!-- 如下:绑定到一个类对象classObject,对象中设置多个key:value对 --> <!-- color为true则color样式起作用。 --> <!-- background为false则background样式不起作用。 --> </div> <script> const app = new Vue({ el:"#app", data:{ message:"你好啊", isColor: true, isBackground: true }, computed: { classObject: function () { return { color: this.isColor, background: this.isBackground } } } }) </script>
4. If the objects in the class are more complex, put them directly in a method, and then call this function to achieve dynamic switching
<!DOCTYPE html> <html> <head> <script type="text/javascript" ></script> <style> .active{ background-color: pink; } .line{ color: red; } </style> </head> <body> <div id="app"> <h2 id="方法methods-message">方法methods:{{message}}</h2> <h2 id="计算属性computed-message">计算属性computed:{{message}}</h2> <button v-on:click="btnClick">按钮</button> </div> <script> const app = new Vue({ el: '#app', data: { message: '你好啊', active:'active', isAcitve:true, isLine:true }, methods:{ btnClick: function () { this.isAcitve = !this.isAcitve },getClasses:function () { return {active:this.isAcitve,line:this.isLine} } }, computed: { classes: function () { return {active:this.isAcitve,line:this.isLine} } } }) </script> </body> </html>
##2. v-bind dynamically binds class attributes (array syntax)
We can pass an array to:class to apply a class List;
:class="[base1,base2]"v-bind Dynamically binding class array syntax is to directly pass in an array, but the array is full of class names. The class name will be added to this tag in the page. The style is changed by adding or subtracting elements in the array.
Note: The class names here need to be wrapped withNote: It is the same as the object syntax and can exist at the same time as an ordinary class without conflict' '
. If not wrapped, Vue will treat it as an attribute in data and go to data Searching inside, obviously there is no such attribute in the data, so the error will come. This is common in Vue. Without quotation marks, it is treated as an attribute in data
<h2 id="message">{{message}}</h2>
<h2 id="message">{{message}}</h2> <h2 id="message">{{message}}</h2> <h2 id="message">{{message}}</h2><script> const app = new Vue({ el: '#app', data: { message: '你好啊', active:'active', line:'bbbbbbbbbb' }, methods:{ getClasses: function () { return [this.active,this.line] } }, computed: { classes: function () { return [this.active,this.line] } } }) </script>方法methods:{{message}}
计算属性computed:{{message}}
<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>Writing like this will always add errorClass, but only add activeClass when isActive is true. However, it is a bit cumbersome to write like this when there are multiple conditional classes. Therefore, object syntax can also be used in array syntax:
<div v-bind:class="[{ active: isActive }, errorClass]"></div>Example:
<!DOCTYPE html> <html> <head> <script type="text/javascript" ></script> <style> .aaa{ padding: 10px; } .active{ background-color: pink; } .line{ color: red; } </style> </head> <body> <div id="app"> <div :class="['aaa','active']">{{message}}</div> <div :class="['aaa', nba, isActive? 'active': '']">{{message}}</div> <div :class="['aaa', nba, {'actvie': isActive}]">{{message}}</div> </div> <script> const app = new Vue({ el: '#app', data() { return { message: "Hello World", nba: 'line', isActive: false } } }) </script> </body> </html>
web front-end development ,Basic Programming Video)
The above is the detailed content of Detailed explanation of how v-bind dynamically binds class attributes. For more information, please follow other related articles on the PHP Chinese website!

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
