简体   繁体   中英

router-link-active Nuxt 3 / VueJS on nested routes not applying to parent

I have created a simple reproduction to better explain myself. I have:

-| pages/
---| test/
------| index.vue
------| nested.vue

And I have a navbar, having read the documentation I assume if I NuxtLink to /test or /test/nested.vue then I would have router-link-active css class applied to both in the navbar but it doesn't seem to do that.

The docs seem to suggest you should be laying out your content as:

-| pages/
---| parent/
------| child.vue
---| parent.vue

I tried that and just doesn't work - the child is never rendered (unless I add another <NuxtPage> to parent.vue which is not what I want since that would show content of parent and child.

Reproduction here:https://stackblitz.com/edit/nuxt-app-config-t3nvjv?file=app.vue

Help would be much appreciated appreciated.

I faced exactly the same issue and finally understood how to use NuxtPage !

You must follow the following structure:

/pages
  /posts
    [postId].vue
    index.vue
  posts.vue

In your /posts/index.vue :

<template>
  <h1>All posts page</h1>
  <p>Whatever...</p>
</template>

In your /posts/[postId].vue :

<template>
  <h1>Single post page</h1>
  <p>{{ postId }}</p>
</template>

And finally, in your posts.vue :

<template>
  <NuxtPage />
</template>

Both your index.vue and [postId].vue content will be rendered separately and your NuxtLink s router-link-active and router-link-exact-active classes will work:)

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