vuejs / router

🚦 The official router for Vue.js
https://router.vuejs.org/
MIT License
3.91k stars 1.19k forks source link

Nested routes not marked as active #2025

Closed Radiergummi closed 11 months ago

Radiergummi commented 11 months ago

Reproduction

https://codesandbox.io/s/nested-views-broken-active-behaviour-yvcshl

Steps to reproduce the bug

  1. Create a route with children routes nested below the parent path, like so:
    {
     path: "/foo",
     component: () => import("parent-component-with-router-view.vue")
     children: [
       { path: "bar", component: () => import("child-component-bar.vue") },
       { path: "baz", component: () => import("child-component-baz.vue") },
     ]
    }
  2. Add router-link instances to the page:
    <router-link to="/foo">/foo</router-link>
    <router-link to="/foo/bar">/foo/bar</router-link>
    <router-link to="/foo/baz">/foo/baz</router-link>
  3. Click one of the children links
  4. Witness the parent link not receiving active state

Expected behavior

Parent links should be marked as active if a child route is active and they do not have an exact prop. I understand this has changed in Vue router 4, but my routes are properly nested.

Actual behavior

The parent route never receives an active state. I have tried directly referencing components, reducing the parent component to a router-view, adding route names, redirects, slashes, what have you. This is simply not working as stated in the documentation.

Additional information

I modified the example from the Vue documentation, which used a route parameter in the parent URL, which apparently is working. This has to work without parameters, too, however. In my specific situation, I have the following routes (indentation indicates children):

For all of these, I'd expect the "/team" navigation item to be active. I created the router configuration as described in the documentation, but it doesn't work.

posva commented 11 months ago

Your repro isn’t using the code sample you are referring to. It’s working as expected given the parameter is repeated across routes rather than be a single route record with children. Are you sure you are not missing something?

Radiergummi commented 11 months ago

Apparently so, although I can't figure out what it is. Sorry for bothering!