Layout and Pages
Hi guys,
I'm trying to have a pages/index.vue that route to components/HomePage.vue when connected and components/LandingPage.vue when non connected
And i also have layouts/default.vue for all pages when connected who contains a menu, and layouts/unauthenticated.vue for the LandingPage.vue only for now:
`
SO i have this code but the part :
definePageMeta({
layout: isAuthenticated.value ? 'default' : 'unauthenticated'
})
Is causing me an error :
index.vue?macro=true:2 Uncaught
ReferenceError: isAuthenticated is not defined
at index.vue?macro=true:2:11
Anyone has an idea ?
3 Replies
Hi @BobLamarley. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The error you're encountering is because
definePageMeta
is evaluated at build time, not at runtime. This means you can't use reactive values or runtime logic inside it. Let me explain how you can achieve what you're trying to do based on the Nuxt documentation.
To dynamically change layouts based on authentication state, you have a couple of options:
1. Use setPageLayout
in a middleware or plugin:
You can create a middleware that checks the authentication state and sets the layout accordingly:
Then, in your pages/index.vue
, you can use this middleware:
2. Use <NuxtLayout>
component in your page:
Alternatively, you can handle the layout change directly in your page component:
This approach allows you to dynamically change the layout based on the authentication state at runtime.
For more information on layouts and how to use them dynamically, you can refer to the Nuxt Layouts documentation.
Remember, the definePageMeta
function is meant for static metadata that's known at build time. For dynamic values that change at runtime, you need to use one of the approaches mentioned above.
Source
Source
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@314138038995255298> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Depending on how much the layout changes, I wouldn't bother much and do the same thing as you did in your page.
Like, displaying a different menu depending on isAuthenticated