Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I see the navigation history in vue-router?

I have a vue.js application and I'd like to see the navigation history in vue-router. I have a beforeRouteLeave hook in my component and I put a breakpoint in there. It hits the breakpoint, and I type this.$router in the console. It outputs the $router object but I don't see an actual stack of urls representing the history of my navigation through the site.

This is what I see:

currentRoute: (...)
app: Vue {_uid: 3, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: Vue, …}
apps: [Vue]
options: {mode: "history", linkActiveClass: "active", routes: Array(47), scrollBehavior: ƒ}
beforeHooks: []
resolveHooks: []
afterHooks: [ƒ]
matcher: {match: ƒ, addRoutes: ƒ}
fallback: false
mode: "history"
history: HTML5History {router: VueRouter, base: "", current: {…}, pending: {…}, ready: true, …}
__proto__: Object

(I could expand this but I think that would be too big.)

Where is the actual navigation stack?

If you want to know why I need to see the navigation stack, it's because, when I click the back button on the browser, I suspect that the navigation stack is being popped even though my beforeRouteLeave hook is called and I don't call next(). I bring up a popup in beforeRouteLeave with the options: "leave" or "stay". If the user clicks "stay", I stay on the page. It seems to do the trick (it doesn't navigate away from the current page), but then if I click refresh on the browser, it refreshes with the previous page. Or if I say "stay" once, then click the back button again and say "leave", I go two pages back.

So I want to see what's going on with the navigation stack. Is it being popped regardless of whether next() is called in beforeRouteLeave, at least when I click the back button? Or is something else going on.

Thanks.

like image 852
Gibran Shah Avatar asked Jan 28 '20 15:01

Gibran Shah


People also ask

How do I view previous routes on vue?

To get previous page URL with Vue Router, we can get it from the from parameter of the beforeRouterEnter hook. to add the beforeRouteEnter hook into our component and get the previous page URL from the from parameter.

What is history in vue router?

HTML5 History Mode The default mode for Vue Router is hash mode. It uses a URL hash to simulate a full URL so that the page won't be reloaded when the URL changes. We can set Vue Router to history mode to get rid of the hash. It uses history.

How do I check my current route on vue?

We can use this. $router. currentRoute. path property in a vue router component to get the current path.

What are navigation guards in vue router?

Navigation guards are a specific feature within Vue Router that provide additional functionality pertaining to how routes get resolved. They are primarily used to handle error states and navigate a user seamlessly without abruptly interrupting their workflow.


2 Answers

It's not currently implemented, though you can use router navigation guards (route change hooks) as a middleware to store the from and to argument objects and write your logic with these values.

Just make sure you call the next() function, otherwise you'll be storing logs and not calling the next route.

Check the docs here: https://router.vuejs.org/guide/advanced/navigation-guards.html

like image 193
Fabio Martins Avatar answered Oct 07 '22 14:10

Fabio Martins


This doesn't directly answer your question about how to see the navigation history in vue-router, but I came across your question with a similar problem that you mentioned as well:

When I click the back button on the browser, I suspect that the navigation stack is being popped even though my beforeRouteLeave hook is called and I don't call next()

It seems the solution to this problem is to explicitly call next(false) according to the official vue-router documentation.

https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards

next(false): abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the from route.

I've tested on vue 2.x and it solves the problem for me in cases where it was navigating to another route: e.g. /a -> /b but not in cases where it's navigating to the same route different param: e.g. /page/1 -> /page/2

like image 1
njachowski Avatar answered Oct 07 '22 16:10

njachowski