0

I have a collection of cards which are iterated. Every card has a color attribute and I want to use this as part of the class of a div to give it a dynamic background. The current code is:

      <div v-for="card in cards" :key="card.id">
        <div class="bg-red-600">{{ card.category | uppercase }}</div>
        <div>{{ card.title }}</div>
      </div>

In this case, in the value bg-red-600, red should be replaced with card.color. How can I do this?

0

1 Answer 1

1

You have many options. In template:

  <div :class="`bg-${card.color}-600`">{{ card.category | uppercase }}</div>

Via separate method:

methods: {
  colorMethod(color) {
    return `bg-${color}-600`;
  },
},

<div :class="colorMethod(card.color)">{{ card.category | uppercase }}</div>

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

@John Nice to hear!

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.