Photo by Paige Cody on Unsplash

How to conditionally bind ONE class to an element in VUE

--

I had a hard time trying to find the answer to this on stack overflow so I thought I would write a quick little article in case any one else is having trouble finding the answer to the same thing.

This may not be the best way to do this … but it worked for me 🙌

If you are only needing to conditionally add one class to an element based on the truthiness of a statement, and you dont want to use a computed property, you can do it like the below:

<div v-bind:class="{ 'my-class': item.unitprice > item.baseunitprice}"></div>

In this example, ‘my-class’ will be added to the div if the unit price is greater than the baseunit price.

Happy Coding! ⌨️

--

--

holly bourneville

Hi Im Holly! I’m from New Zealand. I’m in my first job as a Front End Web Developer and want to share my learnings and thoughts along the way!