I have an Angular2 app with a route like this:
{
path: '',
component: ContentComponent,
children: [
{
path: 'folder/:folderId',
resolve: {
currentFolder: CurrentFolderResolver,
},
children: [
{
path: '',
resolve: {
folderStructure: FolderStructureResolve,
},
component: FolderOverviewComponent,
},
{
path: 'users',
component: UsersComponent,
}
]
}
]
}
When navigating from a route like /folder/123 to /folder/456, Angular will not trigger ngOnDestroy()
in the FolderOverviewComponent
. Navigating to /folder/456/users will do.
In other words, it seems like Angular does not destroy the component if the route does not change (ignoring the dynamic part of :folderId). This seems reasonable, nevertheless I need to clean up things in ngOnDestroy()
.
Can I configure Routes to call destroy each time I navigate to a new route (i.e. with a different parameter)?
ngOnDestroy doesn't get called because some components do not get destroyed when navigating to a different route.
Many developers are surprised to learn that ngOnDestroy is only fired when the class which it has been implemented on is destroyed within the context of a running browser session. In other words, ngOnDestroy is not reliably called in the following scenarios: Page Refresh.
The ngOnDestroy or OnDestroy hook is called just before the Component/Directive instance is destroyed by Angular. Use this hook to Perform any cleanup logic for the Component. This is the correct place where you would like to Unsubscribe Observables and detach event handlers to avoid memory leaks.
ngOnDestroy() gets called when a component is about to be destroyed. Notice how the example "destroys" the child component via the conditional ngIf='showChild'. By toggling the value of showChild, Angular creates and destroys the child component each time this value changes.
That's by design. If only a route parameter changes which leads to the same route being used, the component is not destroyed and recreated but reused.
You can subscribe to params
changes to be able to execute code when the route was changed:
constructor(router: ActivatedRoute) {
router.params.subscribe(param => routeChanged(param['folderId']));
}
There are plans to provide more flexibility in the future, but currently that's the way to go.
See also https://angular.io/guide/router#activated-route-in-action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With