0

I have components structured like this:

   <component-1 
       v-if="this.showData"
    ></component-1>
    
    <component-2
       v-else
    ></compoment-2>

I would like to wrap a transition around each one. Is my only option explicitly dropping the v-else and checking "v-if="!this.showData" like:

   <transition name="fade>
      <component-1 
          v-if="this.showData"
       ></component-1>
   </transition>
    
   <transition name="fade>
      <component-2 
          v-if="!this.showData"
       ></component-1>
   </transition>

or is there another way I can keep my v-if/else -- it just feels cleaner to me.

timmyLtus
  • 274
  • 1
  • 12

2 Answers2

2

You can either make if/else on transition component

<transition name="fade" v-if="this.showData">
  <component-1></component-1>
</transition>
    
<transition name="fade" v-else>
  <component-2></component-1>
</transition>

or move your components under transition

<transition name="fade">
  <component-1 v-if="this.showData"></component-1>
  <component-2 v-else></component-1>
</transition>
  • 1
    Don't forget to use key attributes if transitions are applying for the same tag names. Refer: [Vue 2 Transition not working](https://stackoverflow.com/a/58230103/8031025) – Varun Kumar Medam Jun 19 '21 at 21:57
1
<transition name="fade">
  <component-1 v-if="this.showData" />
  <component-2 v-else />
</transition>

actually you no need to call this in template html.

v-if="this.showData"

can be like this:

v-if="showData"

Fatur
  • 141
  • 9