Class Binding in Vue with Vue v-bind

In this guide, we will look at class binding with the Vue v-bind directive. Class binding comes in handy when we have the need of…

The post Class Binding in Vue with Vue v-bind appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Prince Chukwudire

In this guide, we will look at class binding with the Vue v-bind directive.
Class binding comes in handy when we have the need of manipulating an element’s class list.

We can either pass an object or array to dynamically toggle a class.

Object Syntax

<template>
<div v-bind:class="{ active: isActive == 1 }"></div>
</template>

<script>
export default {
  data() {
    return {
      isActive: 1
    };
  }
};
</script>

<style scoped>
.active{
    background: tomato;
    height: 50px;
    width: 50px;
}
</style>


The above syntax means that the class active would be present on the said element( a div in this instance ) as long as the expression evaluates to true.

Multiple classes can also be bound as seen here:

  <div
    class="static"
    v-bind:class="{ active: isActive, error: hasError }"
  ></div>

Array Syntax
We can as well pass an array to apply a list of classes.

<div v-bind:class="[active, error]"></div>

The post Class Binding in Vue with Vue v-bind appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Prince Chukwudire


Print Share Comment Cite Upload Translate Updates
APA

Prince Chukwudire | Sciencx (2021-02-17T17:26:12+00:00) Class Binding in Vue with Vue v-bind. Retrieved from https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/

MLA
" » Class Binding in Vue with Vue v-bind." Prince Chukwudire | Sciencx - Wednesday February 17, 2021, https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/
HARVARD
Prince Chukwudire | Sciencx Wednesday February 17, 2021 » Class Binding in Vue with Vue v-bind., viewed ,<https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/>
VANCOUVER
Prince Chukwudire | Sciencx - » Class Binding in Vue with Vue v-bind. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/
CHICAGO
" » Class Binding in Vue with Vue v-bind." Prince Chukwudire | Sciencx - Accessed . https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/
IEEE
" » Class Binding in Vue with Vue v-bind." Prince Chukwudire | Sciencx [Online]. Available: https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/. [Accessed: ]
rf:citation
» Class Binding in Vue with Vue v-bind | Prince Chukwudire | Sciencx | https://www.scien.cx/2021/02/17/class-binding-in-vue-with-vue-v-bind/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.