1

I do not understand how to create nested child routes in Nuxt3.

I have a parent file:

pages/index.vue

<template>
  <div>
    <h1>The home page</h1>
    <AppFilter @picked-status="(e) => (status = e)" />
    <NuxtPage />
  </div>
</template>

<script setup>
const status = ref('one');
</script>

and then children like so:

pages/one/index.vue, pages/two/index.vue, and pages/three/index.vue.

<template>
  <h1>Child one</h1>
</template>

The parent file contains radio button navigation that when pressed, should show the relevant child component below. If "Child One" is pressed, I expect to see the pages/one.index.vue component load below the navigation.

enter image description here

but, this does not work...

Perhaps there is a better way to structure the app for my use-case?

Here's my code with a minimally reproduction stackblitz editor here: https://stackblitz.com/edit/nuxt-starter-dmx5zu?file=pages/index.vue

Thank you for any help!

redshift
  • 4,815
  • 13
  • 75
  • 138
  • Give a try to that one: https://v3.nuxtjs.org/guide/directory-structure/pages#dynamic-routes – kissu Nov 07 '22 at 21:54
  • That did not work for me...it won't let me use pages/index.vue as the parent, which is what I am trying to do. Maybe layouts is what I need to look at? – redshift Nov 07 '22 at 23:19
  • 1
    Layouts and pages are pretty much the same thing. And you can totally navigate from `/` to `/two`. – kissu Nov 07 '22 at 23:21
  • I think I got page layouts working to how I want it, but for some reason now radio buttons are not working: https://stackoverflow.com/questions/74361100/nuxt3-why-are-selected-radio-buttons-not-shown-when-using-in-a-layout-component – redshift Nov 08 '22 at 12:56

1 Answers1

-1

There is just one <NuxtPage /> tag in app.vue. pages/index.vue is responsible for displaying home page "/" and only home page. Nesting child components according to routes is not a good idea IMO. If you want to have different Layouts/navigation, according on what route you are, use layouts folder.

This is my opinion. There might be a way to stack components depending on how many routes you have, but I doubt that.

Well, it's not stupid to have one root component nested to everyone in the application. You can use middleware to check the route user want to go before any component will be created, send that information to app.vue to pick correct layout. Or if there are just 1-3 routes, you can hardcode it inside those routes. I'm not sure, but I think if there will be a default layout in app.vue nested route will override it.

Mises
  • 4,251
  • 2
  • 19
  • 32
  • 1
    Ok maybe I need to look at using layouts. I need to have the main `/` homepage show the radio buttons and then the URL address to reflect filtered selection. – redshift Nov 07 '22 at 23:19
  • I think I got it working, but now a new question related to it: https://stackoverflow.com/questions/74361100/nuxt3-why-are-selected-radio-buttons-not-shown-when-using-in-a-layout-component – redshift Nov 08 '22 at 12:57
  • FYI, OP should learn about Nuxt layouts, yes, but... nested child routes _are an official thing_ in Nuxt and would deliver the ux the OP is looking for by using ``, although the file structure would be different (which is probably less important). [Nuxt2 nested routes docs](https://nuxtjs.org/examples/routing/nested-pages) ... and... [Nuxt3 nested routes docs](https://nuxt.com/docs/guide/directory-structure/pages#nested-routes). – Kalnode Apr 13 '23 at 13:26