VueJS 3 - <router-link-active> 类不适用于以相同路径名开头的路由

bab*_*s95 7 javascript vue.js vue-router

我创建了一个简单的 navar,其中有 3 个链接。所有链接均在路由器对象的 ROOT 级别声明。我添加了一个针对<router-link-active>导航栏上突出显示活动链接的类的简单样式。这一切都工作正常,在链接之间切换会更新 URL、更改<router-view>以及将正确的样式应用于当前的导航栏链接。

我遇到的问题是,每当我单击也在路由器对象的根级别上声明的第四个链接时,从与当前活动链接相同的路径名开始,类<router-link-active>灾难。例如

{ path: "/link2", component: link2 },
{ path: "/link2/sibling", component: sibling },
Run Code Online (Sandbox Code Playgroud)

我的理解是,因为/links2/sibling与 开头同名/link2,导航到的导航栏项目/link2仍应具有该类<router-link-active>,即使 是/link2/sibling当前活动的 URL。

代码沙箱

应用程序.vue

<template>
  <div>
    <ul class="flex gap-x-5">
      <router-link to="/">
        <li>Link 1</li>
      </router-link>
      <router-link to="/link2">
        <li>Link 2</li>
      </router-link>
      <router-link to="/link3">
        <li>Link 3</li>
      </router-link>
    </ul>
  </div>
  <router-view />
</template>

<script>
export default {
  name: "App",
};
</script>

<style>
a:hover,
a:active,
a.router-link-active {
  color: #f1a80a;
  border-color: #f1a80a;
  background-color: #1a037e;
}
</style>
Run Code Online (Sandbox Code Playgroud)

main.js

import App from "./App.vue";
import router from "./router.js";

const app = createApp(App);

app.use(router);

app.mount("#app");

Run Code Online (Sandbox Code Playgroud)

路由器.js

import { createRouter, createWebHistory } from "vue-router";
import link1 from "./components/link1.vue";
import link2 from "./components/link2.vue";
import sibling from "./components/sibling.vue";
import link3 from "./components/link3.vue";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: "/", component: link1 },
    { path: "/link2", component: link2 },
    { path: "/link2/sibling", component: sibling },
    { path: "/link3", component: link3 }
  ]
});

export default router;

Run Code Online (Sandbox Code Playgroud)

链接1.vue

<template>
  <div>You are inside Link1</div>
</template>
Run Code Online (Sandbox Code Playgroud)

link2.vue

<template>
  <div>
    <router-link to="/link2/sibling">
      You are inside Link 2 (CLICK ME)
    </router-link>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

link3.vue

<template>
  <div>You are inside Link 3</div>
</template>
Run Code Online (Sandbox Code Playgroud)

兄弟姐妹.vue

<template>
  <div>You are inside Link2 sibling</div>
</template>
Run Code Online (Sandbox Code Playgroud)

ham*_*odi 4

我认为这是我们对路由所期望的自然行为。当您单击You are inside Link 2 (CLICK ME)内部link2.vue组件时,会vue-router加载. 因此该视图中没有查看样式的链接。如果您想查看该样式,则必须将链接保留在视图中并且不允许其消失。sibling.vuerouter-viewApp.vueYou are inside Link 2 (CLICK ME)router-link-activevue-router

为了实现这样的目标,您可以像这样使用嵌套路由vue-router。首先将router.js文件更改为如下内容:

import { createRouter, createWebHistory } from "vue-router";
import link1 from "./components/link1.vue";
import link2 from "./components/link2.vue";
import sibling from "./components/sibling.vue";
import link3 from "./components/link3.vue";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: "/", component: link1 },
    { 
        path: "/link2",
        component: link2,
        children: [
            {
              path: 'sibling',
              component: sibling
            },
          ]
    },
//    { path: "/link2/sibling", component: sibling
//        
//    },
    { path: "/link3", component: link3 }
  ]
});

export default router;
Run Code Online (Sandbox Code Playgroud)

然后将 a 添加<router-view>到您的link2.vue文件中,如下所示:

<template>
  <div>
    <router-link to="/link2/sibling">
      You are inside Link 2 (CLICK ME)
    </router-link>
    <router-view></router-view>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)