How to get path from route instance?

Route config

{ path: '/user/:id', component: User }

i change param someplace

this.$route.params({ id: 'foo' });

in hash mode, i can get path /#/user/foo
in history mode, get /user/foo

Sorry, but what exactly is your question here?

How to get current route?

Check the object:

console.log(this.$route);

There no the property which i want.

I mean, is there a method as like ‘toPath’ in route instance ? Through the medthod, I can get the current path.

For example, current url is ‘http://somedomain.com/#/user/foo’, then
this.$route.toPath()
will return ‘/#/user/foo’ in hash mode, or ‘/user/foo’ in history mode.

Why not just use native approach?

window.location.href 
1 Like

Well, my need is strange. For some reasons, I can’t use route.push({ name: ‘foo’ }), and must to use route.push({ path: ‘xxxx’, }). Here I want to get the right path through some official ways.

In hash mode, window.location.pathname can only get ‘/’, and I don’t know what the base is, and params …

Why cant you use route.push({ name: ‘foo’ }) ? Whats the error? You dont provide us with your actual issue at hand. You shouldnt need to be doing this long way rounded functionality for route navigation.

If your route config in your original post is anything to go by, the reason {name} is not working for you, is because you have not provided a name to your routes.

I’m not sure about the question so I might be totally wrong, but isn’t this.$route.path what you are looking for?
All route properties: https://router.vuejs.org/en/api/route-object.html#route-object-properties

$route.path is always the path of current route or url.

I want to get a method that can get path dynamically, when I change something, such as param …

The path will be used for $route.push .

I have same problem, have you got any solution?