A
A
ArtJH2021-04-07 10:12:11
JavaScript
ArtJH, 2021-04-07 10:12:11

How to display the value of a condition with a string in vue?

<p :class="'ic' + true ? 'on' : 'off' + '.svg'">123</p>

Displays the tag simply with class="on", without additional text.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-04-07
@ArtJH

Vue has nothing to do with it. Understand operator precedence .
Let's add parentheses to make it clearer what is executed in what order: Well, of course, writing such expressions in a template is not cool at all. Move it to a computed property or method, like so:
('ic' + true) ? 'on' : ('off' + '.svg')

computed: {
  className() {
    return условие ? 'icon.svg' : 'icoff.svg';
  },
  ...

<p :class="className">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question