NuxtN
Nuxt6mo ago
9 replies
Kérunix

nuxt context unavailable in definePageMeta `redirect`

I'm trying to redirect the user using definePageMeta and the redirect function, but my app needs i18n and when I try

definePageMeta({
  redirect: (to) => {
    const { $localePath } = useNuxtApp();
    return $localePath({ name: 'my-page, params: to.params });
  },
});


I get $localePath is not a function. If I replace const { $localePath } = useNuxtApp(); with const localePath = useLocalePath(); I get another error "error caught during app initialization Error: i18n context is not initialized. Ensure the i18n plugin is installed and the composable is used within a Vue component or setup function.".

Is there a way to access localePath in definePageMeta context at all ? If not, how should I tackle i18n and redirection using named routes and not absolute paths ?
Was this page helpful?