【问题标题】:router-link-active Nuxt 3 / VueJS on nested routes not applying to parentrouter-link-active Nuxt 3 / VueJS 嵌套路由不适用于父级
【发布时间】:2022-12-10 06:19:42
【问题描述】:

我创建了一个简单的复制品来更好地解释我自己。我有:

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

我有一个导航栏,阅读了我假设的文档,如果我将 NuxtLink 链接到 /test/test/nested.vue,那么我将在导航栏中将 router-link-active css 类应用于两者,但它似乎没有这样做。

文档似乎建议您将内容布置为:

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

我试过了,但没有用——孩子永远不会被渲染(除非我在 parent.vue 中添加另一个<NuxtPage>,这不是我想要的,因为那会显示父母和孩子的内容。

转载在这里:https://stackblitz.com/edit/nuxt-app-config-t3nvjv?file=app.vue

帮助将不胜感激。

【问题讨论】:

    标签: javascript vue.js nuxt.js vue-router nuxtjs3


    【解决方案1】:

    我遇到了完全相同的问题,终于明白了如何使用NuxtPage

    您必须遵循以下结构:

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

    在你的/posts/index.vue

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

    在你的/posts/[postId].vue

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

    最后,在你的posts.vue

    <template>
      <NuxtPage />
    </template>
    

    您的index.vue[postId].vue 内容将单独呈现,并且您的NuxtLinks router-link-activerouter-link-exact-active 类将起作用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 2016-11-11
      • 1970-01-01
      • 2017-10-02
      • 2022-11-16
      • 2020-11-21
      • 2018-06-27
      • 2021-12-09
      相关资源
      最近更新 更多