The lifecycle event can be used to control router navigation or know different router actions.
Different router lifecycle are -
All lifecycle are emitted as events, so it can be listened in outside as well as inside component.
beforeEach is called after route is found. It can result a boolean result or route.
The beforeEach
event contains new route as argument.
router.on("beforeEach",(to)=>{
console.log("beforeEach",to);
});
if the target route does not exist then notFound event is emitted
notFound
is called when no route is found. It is called with to
route.
router.on("notFound",(to)=>{
console.log("notFound",to);
});
navigate is called after beforeEach is resolved. The event indicates route is being changed. It contains the new route as arguments.
router.on("navigate",(to)=>{
console.log("navigate",to);
});
afterEach is called after navigate is resolved. It indicates change of route. It is called with to
route, from
route and error
if any.
router.on("afterEach",(to, from, error)=>{
console.log("afterEach",to, from);
if(error){
console.error("error", error);
}
});