javascript - VueJS 3 - <router-link-active> class not applied to routes that start with the same path name - Stack

I have created a simple navar where I have 3 links. All links are declared at a ROOT level of the route

I have created a simple navar where I have 3 links. All links are declared at a ROOT level of the router object. I've added a simple styling targeting the <router-link-active> class where the active link is highlighted on the navbar. This all works fine, switching between links updates the URL, changes the <router-view> as well as applies correct style to the navbar link currently on.

The issue I'm having is that whenever I click on a 4th link which is also declared on the ROOT level of the router object, starting with the same path name as currently active link, the <router-link-active> class disasters. e.g.

{ path: "/link2", ponent: link2 },
{ path: "/link2/sibling", ponent: sibling },

My understanding is because the /links2/sibling starts with the same name as /link2, the navbar item that navigates to /link2 should still be have the <router-link-active> class, even when the /link2/sibling is currently active URL.

Codesandbox

App.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>

main.js

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

const app = createApp(App);

app.use(router);

app.mount("#app");

router.js

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

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

export default router;

link1.vue

<template>
  <div>You are inside Link1</div>
</template>

link2.vue

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

link3.vue

<template>
  <div>You are inside Link 3</div>
</template>

sibling.vue

<template>
  <div>You are inside Link2 sibling</div>
</template>

I have created a simple navar where I have 3 links. All links are declared at a ROOT level of the router object. I've added a simple styling targeting the <router-link-active> class where the active link is highlighted on the navbar. This all works fine, switching between links updates the URL, changes the <router-view> as well as applies correct style to the navbar link currently on.

The issue I'm having is that whenever I click on a 4th link which is also declared on the ROOT level of the router object, starting with the same path name as currently active link, the <router-link-active> class disasters. e.g.

{ path: "/link2", ponent: link2 },
{ path: "/link2/sibling", ponent: sibling },

My understanding is because the /links2/sibling starts with the same name as /link2, the navbar item that navigates to /link2 should still be have the <router-link-active> class, even when the /link2/sibling is currently active URL.

Codesandbox

App.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>

main.js

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

const app = createApp(App);

app.use(router);

app.mount("#app");

router.js

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

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

export default router;

link1.vue

<template>
  <div>You are inside Link1</div>
</template>

link2.vue

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

link3.vue

<template>
  <div>You are inside Link 3</div>
</template>

sibling.vue

<template>
  <div>You are inside Link2 sibling</div>
</template>
Share Improve this question asked Feb 18, 2022 at 16:42 babis95babis95 6221 gold badge8 silver badges32 bronze badges 1
  • I have this problem to, did you solved it ? – morteza mortezaie Commented Aug 14, 2022 at 14:04
Add a ment  | 

1 Answer 1

Reset to default 5

I think that is the natural behavior that we could expect from routing. when you click on You are inside Link 2 (CLICK ME) inside link2.vue ponent, the vue-router loads sibling.vue in the router-view part in your App.vue. So there is no You are inside Link 2 (CLICK ME) link in that view to see the router-link-active styles. If you want to see that styles, you must keep your link in the view and don't allow vue-router to disappear that.

For achieving such a goal you can use Nested Routes in vue-router like this. First change your router.js file to something like this:

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

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

export default router;

And then add a <router-view> to your link2.vue file like this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634538a4637309.html

相关推荐

  • SVN服务器安装 - Windows系统

    文章目录 前言1 下载安装1.1 下载安装包1.2 软件安装1.3 异常处理 2 仓库创建3 权限分配 前言 Windows系统下SVN服务器搭建及遇到相关问题和处理方法记录 1 下载安装 1.1 下载安装包 Windows系统中的S

    1小时前
    00
  • Windows下配置Golang开发环境,并安装配置GoLand IDE

    作者&#xff1a;非妃是公主 专栏&#xff1a;《Golang》 博客地址&#xff1a;https:blog.csdnmyf_666 个性签&#xff1a;顺境不惰&#xff0c;逆境不馁

    1小时前
    00
  • 论文检测,文章检测,降AI率的工具

    1:文字滚筒鸭文字滚筒鸭作为市面上少见的全免费平台,文字滚筒鸭支持文章 AI 率检测、头条文章原创度分析、作文质量评估等多元场景,甚至能一键对比原文与改写后的相似度,所有功能统统 0 收费!精准算法秒出检测结果,不用充值会员,也无需分享裂变

    1小时前
    20
  • 如何增加 Elasticsearch 中的主分片数量

    要增加现有索引的主分片数量,直接修改是不可能的。因此,如果你想增加主分片的数量,必须重新创建索引。通常有两种方法:_reindex API 和 _split API。在这两种方法中,_split API 通常比 _reindex API 更

    1小时前
    20
  • html制作一个放烟花动画的网页代码

    以下是一个用HTML制作的烟花动画网页代码,结合了粒子效果和重力模拟:html<!DOCTYPE html><html><head><title>烟花秀<title>

    1小时前
    20
  • 流固耦合:基本概念、适用软件及 Abaqus 与 Powerflow 的协同仿真

    在工程和科学研究的诸多领域,流固耦合现象广泛存在且对系统性能有着关键影响。理解流固耦合的本质及其应用,对于优化设计、保障安全运行意义重大。同时,借助专业的流固耦合软件,能够更高效地对相关问题进行分析与模拟。接下来,让我们深入探究流固耦合的奥

    1小时前
    00
  • 打破常规!支付宝小程序地图功能开发实用技巧,拓展业务场景

    打破常规!支付宝小程序地图功能开发实用技巧,拓展业务场景嘿,各位开发者小伙伴们

    1小时前
    00
  • AI生态暗战升级,科技巨头铁幕下的终极博弈

    一场围绕AI与智能体的标准、协议及生态的暗战已然蓄势待发。在美剧《权力的游戏》中,不到终局,主角归属始终成谜。如今的AI行业,正上演着同样扣人心弦的戏码。这并非是传统意义上的军事或政治博弈,而是一场围绕AI与智能体的标准、协议及生态展开的暗

    1小时前
    00
  • 10个 DeepSeek 神级提示词,建议收藏!

    在当下人工智能飞速发展的时代,DeepSeek 作为一款功能强大的 AI 工具,能够帮助我们实现各种创意和需求。然而,要充分发挥它的潜力,掌握一些巧妙的提示词至关重要。今天,就为大家精心整理了 15 个 DeepSeek 神级提示词,涵盖多

    45分钟前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    40分钟前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    39分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    27分钟前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    26分钟前
    00
  • Power BI 无公式实现帕累托图表

    帕累托分析(Pareto Analysis),也被称为8020法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表)

    18分钟前
    00
  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因&#xff0c;重装了好几次电脑&#xff0c;因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复&#xff0c;在网上学会了怎么不卸载重装数据库&#xff0c;自己记录以备后面自己查

    13分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

    13分钟前
    00
  • UCB的硅光MEMS OCS控制技术

    昨天写的旭创与nEye合作了一个64端口硅光OCS一、光电路交换技术概述(一)电交换与分组交换电路交换的概念源于早期的电话交换机,通过物理连接实现设备间的通信,就像早期打电话时,接线员手动连接线路一样。而分组交换则是

    6分钟前
    00
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    5分钟前
    00
  • module &#x27;torch.

    踩坑Ascend, 安装 pytorch 2.5.1 和 pytorch_npu 2.5.1, import torch 报错.执行 python -c "import torch;import torch_npu;"时

    3分钟前
    00
  • 重装系统只影响C盘吗?深入解析系统重装的全过程

    重装系统只影响C盘吗?深入解析系统重装的全过程 在计算机的日常使用中,重装系统是一个常见的操作,尤其是在系统出现故障、感染病毒或需要优化系统性能时。然而,许多用户对于重装系统的具体过程和影响存在误解,认为重装系统仅仅是对C盘进行清空和重置

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信