I'm trying to give the specific link "Event" an active class when visiting a nested view link.
For example /event/TER63D/tickets but that doesn't seem to work with the standard vue router. I've found some solutions but these seem to be for older versions or simply don't work.

应用导航模板

<template>
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/event">Event</router-link> (I want this to have the class "active" when visiting /event/TER63D/tickets) 
  </nav>
  <router-view />
</template>

路由

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/event/:id",
    name: "eventlist",
    component: EventView,
    children: [
      {
        path: "tickets",
        component: TicketsView,
      },
      {
        path: "shop",
        component: ShopView,
      },
    ],
    props: true,
  },
  {
    path: "/404",
    name: "404",
    component: () => import("../views/NotFoundView.vue"),
  },
  {
    path: "/:catchAll(.*)", // Unrecognized path automatically matches 404
    redirect: "/404",
  },
];

100

我让它以我想要的方式工作,但我不知道这是不是正确的方式.我对VueJs非常陌生.

这是更新后的代码.

应用导航模板

<template>
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link :class="currentlyActive[0] === 'event' ? 'router-link-exact-active' : ''" to="/event">Event</router-link> (I want this to have the class "active" when visiting /event/TER63D/tickets) 
  </nav>
  <router-view />
</template>

<script>
export default {
  name: "AppView",
  data() {
    return {
      currentlyActive: "",
    };
  },
  watch: {
    $route(to) {
      this.currentlyActive = to.name.split(".");
    },
  },
};
</script>

路由

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/event/:id",
    name: "event",
    component: EventView,
    children: [
      {
        path: "tickets",
        component: TicketsView,
        name: "event.tickets",
      },
      {
        path: "shop",
        component: ShopView,
        name: "event.shop",
      },
    ],
    props: true,
  },
  {
    path: "/404",
    name: "404",
    component: () => import("../views/NotFoundView.vue"),
  },
  {
    path: "/:catchAll(.*)", // Unrecognized path automatically matches 404
    redirect: "/404",
  },
];

推荐答案

此 struct 中没有事件路径和页面.

您可以添加事件路由,如底部.

路由

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
   {
      path: '/event',
      redirect: "/404", // or component: EventList,
      children: [
        {
          path: ":id",
          name: "eventlist",
          component: EventView,
          children: [
            {
              path: "tickets",
              component: TicketsView,
            },
            {
              path: "shop",
              component: ShopView,
            },
          ],
          props: true,
        },
      ]
    },
  {
    path: "/404",
    name: "404",
    component: () => import("../views/NotFoundView.vue"),
  },
  {
    path: "/:catchAll(.*)", // Unrecognized path automatically matches 404
    redirect: "/404",
  },
];

应用导航模板

<template>
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/event">Event</router-link> (I want this to have the class "active" when visiting /event/TER63D/tickets) 
  </nav>
  <router-view />
</template>

Vue.js相关问答推荐

Vuetify 3 v-radio-group 将模型设置为 null

将媒体源/流绑定到视频元素 - Vue 3 组合 API

如何在Vue js 3中根据API响应替换react 值

拖入 vue2-google-map 后如何获取标记位置?

Vuetify 复选框:如何停止点击事件?

在 Vue 项目之间共享assets和组件

如何在Typescript语言Vuejs中使用watch功能?

在 v-for 的情况下如何从 v-model 输入更新 Vuex 存储

在 vuejs 中将旧代码修改为 vue router 4

Django Rest 框架、CSRF 和 Vue.js

store 的 Vuex Classic 模式已弃用,将在 Nuxt 3 中删除

如何禁用 nuxt 默认错误重定向

在加载数据之前触发mounted方法 - VueJS

我在 Nuxt 2.14.0 中运行的是什么版本的 Vue?

重新加载页面正在 vue 组件中的插槽上闪烁内容

Vuejs获取事件正在调用的元素?

标签中的 href 赋值

组件已注册但未使用 vue/no-unused-components

v-for 中的计算(Computed)/动态(Dynamic) v-model 名称

Vue 3 组合 API 数据()函数