Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue-router: how to check if current route is a route or one of its subroutes?

I'm wrapping router-link to add some features.

I need to check if current route is a route or a subdolder of a route.

Something like the following

I know it cannot works, because :active need a boolean, but javascript String.match returns an array;: it's to explain my goal

:active="$route.name.match('customer_users*')"

In my router.js I definied that /customer/{customer_id}/users is named customer_users and that /customer/{customer_id}/users/{user_id} is named customer_users_edit.

So i'd like to be able to set ":active" in my wrapper for both these routes.

like image 523
realtebo Avatar asked Jun 03 '19 10:06

realtebo


People also ask

What is meta in route Vue?

Sometimes, you might want to attach arbitrary information to routes like transition names, who can access the route, etc. This can be achieved through the meta property which accepts an object of properties and can be accessed on the route location and navigation guards.

Should you use history mode for VUE router?

The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes. To get rid of the hash, we can use the router's history mode, which leverages the history.


1 Answers

Use the $route.matched property to see if the current route is your customer_users or any of its children.

For example, using Array.prototype.some()

:active="$route.matched.some(({ name }) => name === 'customer_users')"

See https://router.vuejs.org/api/#route-object-properties

$route.matched

type: Array<RouteRecord>

An Array containing route records for all nested path segments of the current route.

like image 158
Phil Avatar answered Sep 21 '22 07:09

Phil