vuejs / router

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

Make it easier to programmatically tell if a RouteLocationRaw is for the current route #2044

Closed AustinGrey closed 9 months ago

AustinGrey commented 9 months ago

What problem is this solving

When writing a menu, it's common to highlight or otherwise show as "active" the menu option which corresponds to the current route.

Libraries like PrimeVue allow you to specify a RouteLocationRaw when defining a menu option, as that's the "user facing" interface for defining a route to go to. So when writing your own menu, you need to somehow compare a RouteLocationRaw to the currentRoute, which is a RouteLocationNormalizedLoaded. But those two interfaces are wildly different, making comparison fraught with error.

VueRouter already must implement this comparison, since if you try to navigate to a RouteLocationRaw that goes to the same route, you get a navigation failure. But it doesn't expose any methods to allow this.

Proposed solution

Some ideas I have are:

Describe alternatives you've considered

No response

posva commented 9 months ago

Expose a method for turning a RouteLocationRaw into a RouteLocationNormalized, making comparison straightforward.

You should be able to use useLink() or router.resolve() for that already.