-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to hook in afterEach after the transition #1968
Comments
There are indeed some improvements to have in the navigation cycle to allow users branch anywhere. Specifically when dealing with transitions |
I wonder if there is any temporarily workarounds? Without hard-coding something like |
Leverage router.afterEach((to, from) => {
Vue.nextTick(() => {
ga(...)
})
}) |
@studstill However, in the case where Even with |
Any updates on this? I'm having the same issues. |
I'm closing this in favor of #2079 |
@posva sorry for hijacking this issue, hope you don't mind. For anyone looking into this issue with nuxt 3 - vue-router stack , and trying to send events to GA after the DOM title has been updated (for example) vue-router is not where you should look into! import { getActiveHead } from "unhead";
export const useHeadChange = (cb: (() => Promise<void>) | (() => void)) => {
const lastDomRenderState = useState("unhead:dom-render-state", () => ({}));
const head = getActiveHead();
head?.hooks.addHooks({
"dom:rendered": async (ctx) => {
const stringifiedCtx = JSON.stringify(ctx);
if (lastDomRenderState.value !== stringifiedCtx) {
lastDomRenderState.value = stringifiedCtx;
await cb();
}
},
});
}; Use this composable in App.vue like so: <script setup lang="ts">
useHeadChange(() => {
console.log(document.title)
})
</script>
|
What problem does this feature solve?
When implementing analytics with vue-router, we often rely on the
router.afterEach
hook. For example:However, when there is a page transition (and obviously the page transition will use
mode="out-in"
), the new component will wait for the old component to fade out first, but theafterEach
hook was already fired.In the about example, say the user was originally on the home page with page title "home" and clicks on the
/about
link:ga
called to track pageview, but got the old title "home" by callingdocument.title
.home
page fades out, and finally destroyed.about
page gets mounted (and fades in), page title and other metadata get modified (in my case I'm using vue-metaIn this comment, @posva recommend using
Vue.nextTick
. I tried adding multiplenextTick
s but doesn't work...What does the proposed API look like?
Is it possible to perhaps dynamically register a mounted hook (without overriding original one) to the
to
component in afterEach?For example:
Or maybe add a new hook that will wait for page transition to finish, and the new component mounted?
Thank you all so much!!!
Dax
The text was updated successfully, but these errors were encountered: