search
HomeWeb Front-endVue.jsDetailed explanation of how v-bind dynamically binds class attributes
Detailed explanation of how v-bind dynamically binds class attributesAug 10, 2022 pm 02:05 PM
vueDynamically bound propertiesv-bind

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!

Detailed explanation of how v-bind dynamically binds class attributes

#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 work

  • If 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>

Detailed explanation of how v-bind dynamically binds class attributes

v-bind:class directive can also coexist with the ordinary class attribute.

<div
  class="static"
  v-bind:class="{ active: isActive, &#39;text-danger&#39;: hasError }"
></div>
data: {
  isActive: true,
  hasError: true
}

Detailed explanation of how v-bind dynamically binds class attributes

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
}

Detailed explanation of how v-bind dynamically binds class attributes

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>

Detailed explanation of how v-bind dynamically binds class attributes

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>

Detailed explanation of how v-bind dynamically binds class attributes

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: &#39;#app&#39;,
		data: {
			message: &#39;你好啊&#39;,
			active:&#39;active&#39;,
			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>

Detailed explanation of how v-bind dynamically binds class attributes

##2. v-bind dynamically binds class attributes (array syntax)

We can pass an array to

:class to apply a class List;

The array syntax format is:

: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 with

' '. 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

Note: It is the same as the object syntax and can exist at the same time as an ordinary class without conflict

<h2 id="message">{{message}}</h2>

Detailed explanation of how v-bind dynamically binds class attributes

Example:




    
    



    
<h2 id="message">{{message}}</h2> <h2 id="message">{{message}}</h2> <h2 id="message">{{message}}</h2>

方法methods:{{message}}

计算属性computed:{{message}}

<script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;你好啊&#39;, active:&#39;active&#39;, line:&#39;bbbbbbbbbb&#39; }, methods:{ getClasses: function () { return [this.active,this.line] } }, computed: { classes: function () { return [this.active,this.line] } } }) </script>

Detailed explanation of how v-bind dynamically binds class attributes

If you also want to switch classes in the list based on conditions, you can use a ternary expression:

<div v-bind:class="[isActive ? activeClass : &#39;&#39;, 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="[&#39;aaa&#39;,&#39;active&#39;]">{{message}}</div>
    <div :class="[&#39;aaa&#39;, nba, isActive? &#39;active&#39;: &#39;&#39;]">{{message}}</div>
    <div :class="[&#39;aaa&#39;, nba, {&#39;actvie&#39;: isActive}]">{{message}}</div>
</div>
<script>
	const app = new Vue({
		el: &#39;#app&#39;,
		data() {
			return {
			  message: "Hello World",
			  nba: &#39;line&#39;,
			  isActive: false
			}
      }
	})
</script>

</body>
</html>

Detailed explanation of how v-bind dynamically binds class attributes

(Learning video sharing:

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
Vue常见面试题汇总(附答案解析)Vue常见面试题汇总(附答案解析)Apr 08, 2021 pm 07:54 PM

本篇文章给大家分享一些Vue面试题(附答案解析)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5 款适合国内使用的 Vue 移动端 UI 组件库5 款适合国内使用的 Vue 移动端 UI 组件库May 05, 2022 pm 09:11 PM

本篇文章给大家分享5 款适合国内使用的 Vue 移动端 UI 组件库,希望对大家有所帮助!

vue中props可以传递函数吗vue中props可以传递函数吗Jun 16, 2022 am 10:39 AM

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

手把手带你利用vue3.x绘制流程图手把手带你利用vue3.x绘制流程图Jun 08, 2022 am 11:57 AM

利用vue3.x怎么绘制流程图?下面本篇文章给大家分享基于 vue3.x 的流程图绘制方法,希望对大家有所帮助!

聊聊vue指令中的修饰符,常用事件修饰符总结聊聊vue指令中的修饰符,常用事件修饰符总结May 09, 2022 am 11:07 AM

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?React和Vue项目的解决方法浅析如何覆盖组件库样式?React和Vue项目的解决方法浅析May 16, 2022 am 11:15 AM

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

通过9个Vue3 组件库,看看聊前端的流行趋势!通过9个Vue3 组件库,看看聊前端的流行趋势!May 07, 2022 am 11:31 AM

本篇文章给大家分享9个开源的 Vue3 组件库,通过它们聊聊发现的前端的流行趋势,希望对大家有所帮助!

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft