I added a new route and added an animation to transition to the new route.
I added the following code which will push the new route (/first) when the button is clicked:
/* From the Template */ <router-view @clickedNext1="onClickTransition" v-slot="{ Component }"> <transition name="route1" mode="out-in"> <component :is="Component"></component> </transition> </router-view> /* From the Script */ methods: { onClickTransition() { this.$router.push("/first"); },
Now the problem is that when I click the button and call the "onClickTransition" method, the router seems to be pushed fine, but the page is empty. The component is rendered only when I refresh the page manually by pressing ctrl R.
I believe the problem may come from the insertion of the animation, but if I refresh the page manually, the animation works just fine. So I don't know what the problem is. I will be very grateful for your help.
This is the rest of the code for app.vue:
<template> <router-view @clickedNext1="onClickTransition" v-slot="{ Component }"> <transition :key="$route.fullPath" name="route1" mode="out-in"> <component :is="Component" /> </transition> </router-view> </template> <script> export default { name: "App", components: {}, data() { return {}; }, methods: { onClickTransition() { this.$router.push("/first"); }, leave(event) { event.preventDefault(); event.returnValue = ""; }, }, mounted() { window.addEventListener("beforeunload", this.leave); }, beforeUnmount() { window.removeEventListener("beforeunload", this.leave); }, }; </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #fff; background-color: #151515; position: relative; } * { margin: 0; padding: 0; box-sizing: border-box; } body { margin: 0px; } /* route transition */ .route1-enter-from { opacity: 0; } .route1-enter-active { transition: all 3s ease-in; } .route1-leave-to { opacity: 0; } .route1-leave-active { transition: all 3s ease-in; } </style>
Code part in index.js:
import { createRouter, createWebHistory } from "vue-router"; import MainPage from "../views/MainPage.vue"; import FirstScene from "../views/FirstScene.vue"; const routes = [ { path: "/", name: "main", component: MainPage, }, { path: "/first", name: "first", component: FirstScene, }, ]; const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes, }); export default router;
The "onClickTransition" method comes from the "PreStartPage.vue" component, which is a subcomponent of the main route "MainPage.vue".
Once the "Next" button is clicked in "PreStartPage.vue", it will send an event to "MainPage.vue" using this.$emit."MainPage.vue" and then receive it using a method named The event "onClickNext1", which sends a signal to "App.vue" through another this.$emit. This is the source of the "@clickedNext1" displayed in App.vue.
The following is the code in "PreStartPage.vue":
<script> export default { name: "PreStartPage", methods: { onClickNext() { this.$emit("clickedNext"); }, }, }; </script>
This is the code from "MainPage.vue":
<script> import PreStartPage from "../components/PreStartPage.vue"; export default { name: "MainPage", components: { PreStartPage }, data() { return { showMain: true, showPre: false }; }, methods: { toggleMain() { this.showMain = !this.showMain; this.showPre = !this.showPre; }, onClickNext1() { this.$emit("clickedNext1"); }, }, }; </script>
Try modifying your code like this:
The "key" attribute set to $route.fullPath should ensure that the translation is done correctly whenever the route changes.
edit
To solve this problem, you can add the ":enter-active-class" and ":leave-active-class" attributes to the transition component, which allows you to specify the class that should be applied to the element during the transition.
In the App.vue component, you can update the transform component like this:
This will ensure that the correct class is applied to the element during transitions and that the component is fully rendered before the animation begins.
For more information I should you visit the official wiki: https://vuejs.org/guide/built-ins/transition.html#css-based-transitions