简体   繁体   中英

Dynamic routing is not working in nuxt?

I want to have(created) dynamic route like as provided in nuxt docs

pages/projects/
              /_id.vue
              /_id/details.vue

But everytime I call url localhost/projects/1/details/ , it is rendering _id.vue page instead of /_id/details.vue !! How to do correctly ?

Your _id.vue structure should contain <nuxt-child> component in order to render it's child details.vue correctly.

Your folder structure then looks like this:

└── pages
    └── projects
        ├── _id
        │   └── details.vue
        └── _id.vue

Having this folder structure produces router with setup like this:

routes: [
    {
        path: "/projects/:id?",
        component: ProjectsId,
        name: "projects-id",
        children: [
            {
                path: "details",
                component: ProjectsIdDetails,
                name: "projects-id-details"
            }
        ]
    },

That means your code could look something like this:

_id.vue

<template>
  <div>
    <h1>(_id.vue) Project: {{ $route.params.id }}</h1>
    <nuxt-child/>
  </div>
</template>

<script>
export default {}
</script>

<style>
</style>

_id/details.vue

<template>
  <div>
    (_id/details.vue) Project: {{ $route.params.id }}
  </div>
</template>

<script>
export default {}
</script>

<style>
</style>

TIP: If you come to situation where you are not sure about your routing, open your .nuxt/router.js after nuxt build is done. There you will find function createRouter() which will help you debug routes.

You have to config Nuxt-Child. Take a look https://nuxtjs.org/api/components-nuxt-child

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM