2

My vue component is like this :

<template>
    <a class="btn btn-block" :class="[response == 'responseFound' ? ' btn-yellow' : ' btn-default']">
    ...
    </a>
 </template>

It works

But, I want to combine it to be one class

I try like this :

<template>
    <a :class="'btn' [response == 'responseFound' ? ' btn-yellow' : ' btn-default'] ' btn-block'">
    ...
    </a>
 </template>

But it does not work

How can I solve it?

3
  • Why do you want to combine them? This is the correct way of doing it. Also it doesn't need to be an array since you only have one class you're setting. Commented May 3, 2017 at 11:53
  • Write code like in javascript : :class="'btn ' + ((response == 'responseFound') ? 'btn-yellow' : 'btn-default') + ' btn-block'"> Commented May 3, 2017 at 11:57
  • Yup why do you want to combine, vue does that for you. The first process you followed is the right one. Commented May 3, 2017 at 11:59

2 Answers 2

4

Everything inside :class or v-bind:class is an expression. So:

<template>
    <a :class="'btn' + ( response == 'responseFound' ? ' btn-yellow' : ' btn-default') + ' btn-block'">
    ...
    </a>
 </template>
Sign up to request clarification or add additional context in comments.

Comments

1

You can also combine different binding styles in an array:

<a :class="'btn btn-block', [response == 'responseFound' ? ' btn-yellow' : ' btn-default']">

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.