Lifecycle hooks

Route components (view-models) can implement several lifecycle hooks to respond to navigation events:

canActivate

Called before activating the route. It can be used for authorization or to check if data is available.

canActivate(params: any, routeConfig: RouteConfig, navigationInstruction: NavigationInstruction): boolean | Promise<boolean> {
  if (params.id === '42') {
    return true;
  }
  return false;
}

activate

Called when the route is activated. Use this to load data or perform setup.

activate(params: any, routeConfig: RouteConfig, navigationInstruction: NavigationInstruction): void | Promise<void> {
  this.userId = params.id;
  return this.userService.getUser(this.userId).then(user => this.user = user);
}

canDeactivate

Called before navigating away from the route. Can be used to prevent navigation if there are unsaved changes.

deactivate

Called when navigating away from the route. Use this for cleanup.

Last updated

Was this helpful?