Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - ngOnDestroy() not called on similar route

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)?

like image 612
user1024112 Avatar asked Nov 18 '16 13:11

user1024112


People also ask

Why ngOnDestroy is not getting called?

ngOnDestroy doesn't get called because some components do not get destroyed when navigating to a different route.

Does ngOnDestroy get called on page refresh?

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.

When ngOnDestroy method is called?

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.

What is ngOnDestroy ()?

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.


1 Answers

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

like image 109
Günter Zöchbauer Avatar answered Sep 28 '22 07:09

Günter Zöchbauer