Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get route by name and params for vue-router

Tags:

I am using Vue with vue-router. For product items in a list view I would like to generate JSON-LN annotations with the url attribute set to the path of the product detail view.

I know I can get the current route's path by using this.$route.path but is there a way to get a distinct route path as it would be rendered with

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

to inject the route's path somewhere else?

like image 439
Till Kolter Avatar asked Mar 21 '17 12:03

Till Kolter


People also ask

How do I get parameters on my vue router?

In the composition API, we use the useRoute hook. to get the value of the id route parameter with this. $route.params.id with the options API. We call useRoute to return the route object and then we get the value of the id route parameter with route.params.id .

How do I watch my vue route?

To listen for route change events with Vue. js and Vue Router, we can watch the $route object in our component. to watch the $route in our component. We set deep to true to watch for all changes in the $route object's contents.

How do I pass optional parameters in vue router?

You can do that by adding a parameter and making it optional. { path: '/users/:id? ', // id is optional components: Users, ... } To make a query parameter optional – you need to add a question mark "?" after the parameter name.

How do I create a dynamic route vue?

Adding dynamic routes in VueUpdate the router/index. js file with the new route in the routes array. Remember to include the import for the Post component. Restart your app and head to localhost:8080/post/dynamic-routing in your browser.


1 Answers

You are looking for the Router instance's resolve method:

Given location in form same as used in <router-link/>, returns object with the following resolved properties:

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

In your case you could 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; 
like image 135
thanksd Avatar answered Oct 12 '22 00:10

thanksd