S
S
Sergey2022-04-14 23:39:58
Vue.js
Sergey, 2022-04-14 23:39:58

Multiple choice?

Tell me if it is possible to select the desired component in the template based on some parameters, let everything depend on the parameter x , if(x === 1) is component A, if 2 is B and so on. I found only one way, use if else if, where we compare the values, and display the desired component. Perhaps there are better ways?

<A v-if="x === 1">
<B v-else-if="x === 2">
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Morev, 2022-04-14
@chelovekmuravei

<component :is="x === 1 ? A : B" />
Also works with strings if components are registered locally/globally/generally from HTML:

<component :is="x === 1 ? 'div' : 'my-component'"></component>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question