How to use layouts (using named slots)?
I want to use the dashboard-default.vue layout inside all the dashboard pages.
In the dashboard-default.vue, there's 2 named slots for header and content.
How do i use these slots inside the dashboard pages?
I tried this pages/dashboard.vue
and inside the pages/dashboard/index.vue:
This is
pages/dashboard.vue
page:
59 Replies
This is my directory structure:
Yeah i used that
The question is how do i pass title from individual pages to the layout?
So answer on the page
Where u wanna use title?
In script or just on template
My pages are
pages/dashboard/index.vue
, pages/dashboard/settings.vue
I set layout in the parent page pages/dashboard.vue
:
the layout accepts one named slot header
I want to pass this header from the pages inside pages/dashboard/index.vue
, pages/dashboard/settings.vue
so that i can set different titles to the layoutyou confusing layout and pages folders. In nuxt, your layout goes in the layout folder, RTD
Nuxt wont know what to resolve,
pages/dashboard/index.vue
or pages/dashboard.vue
These two create the same route in nuxt's router to www.site.com/dashboard
are you saying you can't have both these files at the same time
pages/dashboard/index.vue
, pages/dashboard.vue
?no you can't what is going to be used for the route?
its working fine for me.
I think both are used for the route
if you want different layouts for the site and for the dashboard you need to put those in the layout folder and choose your layout in the pages component
all i have to do is to include <NuxtPage /> inside the dashboard.vue page
no that's not my original question
do you have a file called
layouts/dashboard-defualt.vue
nuxt probably tries to render default layouts, and for the index.vue it will take priority over dashboard.vueyes I have a layout
layouts/dashboard/default.vue
ok anyway, for page titles you add it under the head directive
no i can see that its using the correct layout i specified
<Head>
<Title>My page title</Title>
</Head>
no i am trying to set a title deep inside the body html
you can set the title on any page and use a reference
use a pinia store if it's nested deep
Or just use
definePageMeta({ pageTitle: '...' })
in your page and $route.meta.pageTitle
in your nested component?
Like that
Yes i know you can do this inside nuxt-layout. But i was looking to do this from child pages like i said in the question.
I think we can conclude that this is not possible to do in nuxt
Child pages or components? yes from components you would need to bubble up an event if you are not using pinia. With pinia it's super easy, set the pinia store value in the child component and use that pinia store value in your layout page or wherever
It looks like you haven't understood what i am trying to do. You can refer my original question in the top.
Yeah you right, im still on setting the page title meta tags, you can use teleport to teleport content or components to this header template, is one way
https://nuxt.com/docs/api/components/teleports
It's inside page...
No we cannot cause u still doesn't understand stuff
U use slots inside layout but u need to define them first
This inside layout
you guys haven't read my question properly i think. yeah i am using
<slot name="title" />
inside the layout.There's many ways to skin this cat @Nisthar maybe you can explain your use case, i'm sure there is a simpler way of solving your problem
I think your question doesn't clearly state what you want to do, and you havent included the layout file so we are just going on assumptions. template slots can only by targeted within the tag of said component, so either you create header and content slots for each dashboard page and pass the layout slots all the way down or we dont know what you want to do.
Here some code that might help
u start raging me...
that was ur question
.
and this how u use them
inside page
its not layout
its component inside ur page which define what layout u a using inside page
read docs man, thats a simple thing u just need to read
if thats not what a u looking for then make u question properly
@Nisthar please provide a simple repro to exhibit what you're trying to achieve by showing what you have tried so others can establish the facts. Since it's very clear nobody seems to understand your issue, perhaps you will need to expand or provide the repro aforementioned. FYI you cannot provide slot content other than parent > child.
@Patrik if someone or something is infuriating you, please avoid engaging in the thread, or take a break. It makes for better reading for others.
Sure. sry for that . i added a simple repo to show the issue clearly https://github.com/Nisthar/layout-named-slots
GitHub
GitHub - Nisthar/layout-named-slots: nuxt repo for issue reproduction
nuxt repo for issue reproduction. Contribute to Nisthar/layout-named-slots development by creating an account on GitHub.
Hopefully its clearer now
Yeah i think its not possible to do this currently but it would have been really good if it was possible.
He said repro
idk how i can push code for you give me access to repo
@Nisthar
melasculla
nickname on github
im new to githubyou can clone the repo and make your own copy on the local
changing the repo directly is not good for others
i dont wanna to create own repo just to show u how it works
u can give access only for me and then remove it
or even remove the whole repo
if u wanna know how it works
I mean I aldy cloned it, and all works I even make 2 options for u
I can send u zip if u don't wanna give access
I can create new branch if u don't want me to affect initial code
@Patrik please can you add it to a stackblitz for future reference
fork it in github to your own account and then open it in stackblitz, it connects to your github account
(((
that man should have a reproduction in the first place
Patrik
StackBlitz
Nuxt - Starter - StackBlitz
Create a new Nuxt project, module, layer or start from a theme with our collection of starters.
wdym by reproduction?
cuebit have send u link u didnt seen it...
check this out and u understand
all works as u wished
so you wanted stackblitz/codesandbox, is that it?
i dont wanted it, thats how people show their issues
if they cant explain issue
alright, i didn't know you wanted stackblitz/codesandbox, i usually make git repo
u knew
u just didnt read
just check link
Why are you so angry dude?
cause u say we didnt understand ur question like 1000 times
when actually it was u who cant understand what a we trying to help u
I am not looking for an alternative like this.
The point of the issue is not to repeat code in all the pages.
I understood the code you were providing and its not what i am looking for.
okay then say what u want because i answered your question
How do i use these slots inside the dashboard pages?
u wanna same title on all dashboard pages?Did you look my directory structure as well? The question was basically how do i use these slots inside the dashboard pages that i have.
I know your code works but i am not looking to repeat the code by using nuxtlayout in multiple pages.
man please
i wanna help
just say what u want
its not necessarily layout
just what is your goal
same title on dashboard pages? thats it?
thats only way to use slots of layout
u need to use NuxtLayout component on every page to have access slots in layout
as cuebit said, i think maybe you should take a break.
...
I am sorry dude. But i don't think you are helping like this
wow, how ungrateful, at this point i would just move on. But I want to suggest one last thing, if you could be more specific in what you want to achieve as in an actual use case then we can help you with an actual solution. Your example of changing the title can be done much simpler, but clearly you dont want a title change but a component in there, in that case you can just compare the router path and change visibility in your dashboard for that. Why do people always want to force their square peg into a round hole if there are so many other ways of solving the same problem, but no you want the square peg in the round hole and screw all other suggestions. I'm out...
I think is safe to say the help being requested and the help being offered is not being seen eye to eye.
@Nisthar I would suggest you try Vue discord to seek further advice if you feel the help offered here does not suffice. Slots are not exclusive to Nuxt. Best of luck.
@cuebit Did you look through the code? What did you understand from the question? @joe_black_unlucky if I want to use pinia store, some other layout engine or props to achieve what I am trying to do, I wouldn't have asked this question right? @Patrik have given me an alternative that works but I am not looking for a solution using NuxtLayout inside the pages as I said.
I have already given the directory structure, a similar question asked before, if you still haven't understood the question, you could have asked me.
I have already made it clear what I am trying to do.
The use case is not repeating the same layout code for all the sub pages and being able to change it from a single place.
also this is incorrect.