I am migrating from Vue2 to Vue3, but the transition behavior is different and I am having trouble.
this is vue2 code
https://codesandbox.io/p/sandbox/silent-surf-yth66k?file=%2Fsrc%2FApp.vue
<template>
<div id="app">
<div id="demo">
<button v-on:click="show = !show">Toggle</button>
<template v-if="show">
<transition name="fade">
<p>hello</p>
</transition>
</template>
</div>
</div>
</template>
<script>
export default {
name: "App",
data() {
return { show: true };
},
};
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
</style>
this is Vue3 code
<script>
export default {
data() {
return {
show: true
}
}
}
</script>
<template>
<button @click="show = !show">Toggle</button>
<template v-if="show">
<Transition name="fade">
<p >hello</p>
</Transition>
</template>
</template>
<style>
.fade-enter-active,
.fade-leave-active {
transition: opacity 1.5s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>
In both cases, a template is defined outside of Transition, and the template is used to control the animation. It worked fine with Vue2, but not working with Vue3. Please let me know if there is any way to deal with it.
thank you
I have no idea....
Your vue2 playground doesn't work for me, but if you want the fade transition to work in your vue3 code
This works for me. You can also add appear to toggle the animation on the initial render
If you have the Transition inside the outter v-if then it makes sense that it won't trigger if it's false.