Vue.js 101 - Dynamic Classes and Computed Properties
Tags: javascript, vue, beginners, tutorial
Published: 2021-04-02 11:44:11

---
title: Vue.js 101 - Dynamic Classes and Computed Properties
published: true
description: 
tags: javascript, vue, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ll22z8mea106b4vjdjy7.jpeg
series: Vue 101
---
Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

Everyday I publish what I learn the day before from my Vue.js course.

Click follow if you want to miss nothing.

Without further ado here is a summary of my notes for that day.

<h2>Binding HTML Classes</h2>
A common need for data binding is manipulating an element’s class list and its inline styles. Since they are both attributes, we can use v-bind to handle them: we only need to calculate a final string with our expressions. 

However, meddling with string concatenation is annoying and error-prone. For this reason, Vue provides special enhancements when v-bind is used with class and style. In addition to strings, the expressions can also evaluate to objects or arrays.

We can pass an object to v-bind:class to dynamically toggle classes:
```html
<div :class="{ active: person.isActive }">
  This is the menu option
</div>
```
In that example Vue will set the class to "active" only if the Vue data object person.isActive equal true

The bound object doesn’t have to be inline. You can reference the whole object directly:
```html
<div :class="classObject">
  This is the menu option
</div>
```
Vue will replace the classObject with the object properties that are equal to true:
```js
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}
```

<h2>Computed properties</h2>
Computed properties allow us to define a property that is used the same way as data, but can also have some custom logic that is cached based on its dependencies. You can consider computed properties another view into your data.

```js
const app = Vue.createApp({
    data() {
        return {
            people: [
                { name: 'Mike Taylor', email: 'mike@example.com', isActive: true},
                { name: 'John Carson', email: 'john@example.com', isActive: false},
                { name: 'Peter Sanders', email: 'peter@exemple.com', isActive: true}
            ],
        }
    },
    computed: {
        activePeople() {
            return this.people.filter((person) => person.isActive)
        }
    }
})
```
This computed property can be use like data:
```html
<div v-for="person in activePeople" :key="person.name">
      {{ person.name }} : {{ person.email }}  
</div>
```


<h2>Conclusion</h2>
That's it for today. Tomorrow the journey continue, see you later! If you want to be sure to miss nothing click follow me here or on twitter!

Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>