4

I need to set a class using vue the value in a vue param. Additionally, I need to conditionally set a class based on if a vue param is a certain value. Is it possible to combine the two below functionalities in to one class assignment?

<button :class="'btn btn-primary modal modal-' + modal.id" 
        :class="{'modal-active' : modal.active}">
</button>
0

2 Answers 2

6

You could do it as follows by using an array :

   <div v-bind:class="[{'modal-active' : modal.active}, 'btn btn-primary modal modal-' + modal.id]"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

A more modern approach would be using the computed property to construct an array of class names:

<template>
  <button :class="classNames"></button>
</template>

<script setup>
const classNames = computed(() => ['btn', 'btn-primary', 'modal', `modal-${model.id}`, modal.active && 'modal-active']);
</script>

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.