Global routing hook
1.router.beforeEach
: Before entering the route
router.beforeEach((to, from, next) = > {
next();
});
Copy the code
To: indicates the destination route object to be entered
From: The route object that the current navigation is leaving
Next: Jumps to a new route. The current navigation is interrupted and a new navigation is restarted.
Next (): Goes to the next hook in the pipe. If all hooks are executed, then the navigation state is confirmed.
Jump: next (‘ path address) or next ({path: “‘}) or next ({name:”})
Next (false): interrupts current navigation.
2,router.beforeResolve
: Global parsing guard
Called after beforeRouteEnter
router.beforeResolve((to, from,next) = > {
next();
});
Copy the code
3,router.afterEach
: After the route is entered
router.afterEach((to, from) = >{});Copy the code
Exclusive routing hooks
The call order follows the global front-guard beforeEach
{
path: '/home'.beforeEnter: (to, from, next) = >{ next(); }}Copy the code
Routing hooks in components
1.beforeRouteEnter
: Before entering the route
Call order after exclusive routing guard beforeEnter, no! Can!!!! Returns component instance this, which can be accessed by passing a callback to Next after Mounted
beforeRouteEnter ((to, from, next) = > {
next(vm= >{
// Access component instances through 'VM'
console.log("vm",vm);
});
});
Copy the code
2,beforeRouteUpdate
:
This hook is called when the current route changes but the component is being reused (the route is being reused for the same component), and this hook is called in that case. You can access the component instance this
beforeRouteUpdate ((to, from, next) = > {
next();
});
Copy the code
3,beforeRouteLeave
: Indicates that the route leaves
Called when navigating away from the corresponding route of the component, which can be accessed by the component instance this
beforeRouteLeave((to, from, next) = > {
next();
});
Copy the code
Route navigation parsing process
- Navigation is triggered.
- Called in a deactivated component
beforeRouteLeave
The guards. - Call global
beforeEach
The guards. - Called in a reused component
beforeRouteUpdate
Guard (+ 2.2). - Called in the routing configuration
beforeEnter
. - Parse the asynchronous routing component.
- Called in the active component
beforeRouteEnter
. - Call global
beforeResolve
Guard (+ 2.5). - Navigation confirmed.
- Call global
afterEach
Hook. - Triggering DOM updates (
mounted
). - call
beforeRouteEnter
The created component instance is passed in as an argument to the callback function passed to Next in the guard.