I am looking for a solution to add and remove classes on components/elements outside of the <nuxt/> element in layouts/default.vue during page transitions in a Nuxt project.
As an example, I would like to animate a div in and then out before every page transition. Pseudo code below:
<!-- layouts/default.vue -->
<template>
<site-curtain :class="{ 'is-active': isTransitioning == true }" /> // I would to apply this class during a page transition
<nuxt />
</template>
Is there an obvious way in Nuxt to listen to the page transitions that I am missing? I have looked at vue-router's beforeRouteUpdate navigation guard but not sure if it's suitable?
Any help will be greatly appreciated!
try this (I used this approach in one of my projects and it works fine):
first I defined a mixin as follows:
mixin/transition.js:then imported it in every page that I wanted to use the effect:
pages/about-us.vuethen in the layout page I could listen to the
transitionevent emited before and after the transition is done and get the payload sent with the event:and this is the
dataobject of the layout component:here is the resources I used to put together this solution:
Nuxt transition properties
Vue's javascript transition hooks