Get route by name and parameters for vue-router

I am using Vue with vue-router. For product items in a list view, I would like to generate JSON-LN annotations with an attribute url

set on the path to the product detail view.

I know I can get the current route route using this.$route.path

, but is there a way to get a great route route as it will be displayed using

<router-link :to={name: 'ProductDetail', params: {id: some_id, slug: some_slug}}></router-link>

to enter a route elsewhere?

+3


source to share


1 answer


You are looking for a router instance methodresolve

:

A given location in a form, similar to that used in <router-link/>

, returns an object with the following permitted properties:

{
  location: Location;
  route: Route;
  href: string;
}

      



In your case, you can do something like this to get the url:

let props = this.$router.resolve({ 
  name: 'ProductDetail',
  params: { id: some_id, slug: some_slug },
});

return props.href;

      

+7


source







All Articles