3

I have a code in Vue.js

<li v-for = "record in records" v-bind:key= "record.id">
                  <div class="collapsible-header"><i class="material-icons">date_range</i><h6>{{record.record_date}}</h6> &nbsp &nbsp <span class="chip white-text">

I want to add {{record.record_status_color}} to the span class="chip white-text"

so it should change color depending upon the value of record.record_status_color

how do I do this in Vue.js

basically it should look like this assuming that {{record.record_status_color}} is green

<span class="{{record.record_status_color}} chip white-text">

<span class="green chip white-text">
1
  • 2
    :class="record.record_status_color" Commented Jan 12, 2018 at 16:16

3 Answers 3

2

I'm personally not a fan of having class and :class on one element. You can do all 3 with :class

<span :class="[record.record_status_color, 'chip', 'white-text']">

Equals

<span class="green chip white-text">
Sign up to request clarification or add additional context in comments.

5 Comments

Disagreed. :class should be reserved for dynamically adding/removing/changing class names, not for adding static class names.
Happy to be disagreed with, it's why i prefaced my answer as being preference. Also helps to know all of the ways to achieve the answer to question.
@connexo He is using a dynamic class with record.record_status_color, adding the others at the end just saves having multiple class attributes against the HTML element. IMO this is the correct approach.
Having both class and :class on the same element never leads to multiple class attributes on the same element. Internally :class works using DOMNode.classList.add()/remove(), so this never is a problem.
I'm not on about the rendered dom element, I mean during dev - It could lead to confusion. Each to their own but if a developer came to my code, I'd prefer him to see it as a standard HTML element is to which you'd never have multiple classes. Just my preference as are yours.
1

You can have both class="..." and dynamic binding :class="..." on the same element, where classes listed in class="..." are always on the element, and classes in :class="..." depend on your model and are dynamically added/removed as per what's going on in the model.

In the :class binding you can bind either String, Object or Array:

<span class="green chip white-text" :class="record.record_status_color">

where record.record_status_color: 'green'"> and some CSS defining the looks for .green elements.

Object use for multiple classes, or for applying conditional usage of the class:

classObject: {
  active: true,
  'text-danger': false
}

and then

<div :class="classObject"></div>

which would resolve to

<div class="active"></div>

You can also pass an Array to :class:

data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

and then

<div :class="[ activeClass, errorClass ]"></div>

which will render to

<div class="active text-danger"></div>

You can even use expressions in the Array syntax:

<div :class="[isActive ? activeClass : '', errorClass]"></div>

or

<div :class="[(new Date().getFullYear() > 2018) ? activeClass : '', errorClass]"></div>

More information on the subject, along with these examples, can be found in the official documentation.

https://v2.vuejs.org/v2/guide/class-and-style.html

Comments

0

You can try this:

<span class="green chip white-text" :class="[record.record_status_color]">

https://v2.vuejs.org/v2/guide/class-and-style.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.