Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to default child is not highlighted when another child is active #2724

Comments

@ghost
Copy link

ghost commented Apr 17, 2019

Version

3.0.3

Reproduction link

https://jsfiddle.net/3svnktq2/1/

Steps to reproduce

The setup is that 'parent' is a menu item. When you go to that item, you are redirected to the child state with default parameters. Within that child state you can modify your parameters which should result in keeping the menu item highlighted.

  1. Click on the parent link
    -> Redirected to child state with type parameter 'type1' and parent is highlighted as active state
  2. Click on 'go to child with type2'

What is expected?

You stay in the same child state with a different parameter, keeping the parent link highlighted as the active state.

What is actually happening?

'Parent' is no longer highlighted as the active state. It is only highlighted when the type parameter is 'type1'.


This happens since version 3.0.3 of vue-router. If you change the loaded version of vue-router to 3.0.2, the behaviour is as expected. More history on this issue: #2717.

@posva
Copy link
Member

posva commented Apr 17, 2019

Despite the warning, you need to reference the parent route to get it highlighted.
<router-link :to="{name:'parent'}">/parent</router-link> (but link doesn't work) or use the link /parent which will make the link work correctly

@posva posva added group[router-link redesign] Issues regarding a redesign in current router-link implementation to make it make it more customizab has workaround improvement labels Apr 17, 2019
@posva posva changed the title Parent state is not correctly highlighted as active state when child state with different parameters is active Link to default child is not highlighted when another child is active Apr 17, 2019
@ghost
Copy link
Author

ghost commented Apr 17, 2019

Referencing the parent route (and adding a redirect to the empty child) also results in no highlight when going to the child with a different parameter (https://jsfiddle.net/a5doLpvh/).
So from version 3.0.3 referencing the parent route through name is not possible? I have to reference it through the link?

@posva
Copy link
Member

posva commented Apr 17, 2019

if you used a named link, you are explicitly referencing the child, so it makes sense not to highlight the link if the child is not being displayed

@ghost
Copy link
Author

ghost commented Apr 17, 2019

In https://jsfiddle.net/a5doLpvh/ I am explicitly referencing the parent which has a redirect to its child. I would expect that the link stays highlighted for the same child state but with a different parameter.
Also according to the documentation found in https://router.vuejs.org/api/#exact, I would expect that any route which starts with /parent should make the 'parent' link active. Unless this is only the case with a reference to a path and not to a route name.

@posva
Copy link
Member

posva commented Apr 29, 2019

I'm revisiting this. What happens is that the link points to a redirected url, so it's never active. Activating such behaviour needs a bit more thinking and is part of an RFC. If you have any input for that, it will be highly appreciated once the RFC is out :)

This is a working version with no redirect: https://jsfiddle.net/o59bxs3m/
You could do the redirect manually in the created hook or the beforeEnter guard

@Alcoine
Copy link

Alcoine commented May 4, 2019

Same happening to me. Working correctly with ver 3.0.1.

@posva
Copy link
Member

posva commented May 10, 2019

I'm marking this as a bug, the child should be indeed marked as active, whether there is a redirect or not. Playground for myself or anybody who wants to give this issue a shot: https://jsfiddle.net/posva/fj7ywzxv/

@posva posva removed the group[router-link redesign] Issues regarding a redesign in current router-link implementation to make it make it more customizab label May 10, 2019
Domino9697 added a commit to Domino9697/vue-router that referenced this issue May 20, 2019
Fix active parent link when parent link redirects to a child and when the user navigates between child links

fix vuejs#2724
@posva posva added the has PR label May 20, 2019
posva pushed a commit to Domino9697/vue-router that referenced this issue Jul 5, 2019
Fix active parent link when parent link redirects to a child and when the user navigates between child links

fix vuejs#2724
@posva posva closed this as completed in 64785a9 Jul 5, 2019
@maxim-usikov
Copy link

maxim-usikov commented Jul 25, 2019

@posva Same for parent redirect, i guess: https://codesandbox.io/s/github/maxim-usikov/vue-router-issue-2724

@posva
Copy link
Member

posva commented Jul 25, 2019

@maxim-usikov the redirect should go on a child (see #2717). A route record with both component and redirect is not allowed because redirect means that the route is never reached, therefore its component is never reached. For routes with children, because we are redirecting to a child route, it can work, but the right declaration of the redirect will be on an empty child { path: '', redirect: {...} }

FYI the fix hasn't been released yet

@maxim-usikov
Copy link

@posva ahhh, thank you for explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment