Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable component reusing on route params change

I upgraded angular2 application to stable release (from rc1), and noticed that ngOnDestroy is not triggered when route params are changed.

With previous version, when changing url from match/123 to match/124, onDestroy was called and I was able to do some cleaning (e.g. unsubscribing from websocket for that particular match).

With this new behaviour, I'm not sure what is the best way to do the same? Can I force component to be re-initialized again?

this is parent structure, and mentioned component is rendered withing router-outled

    <div id="content">
      <live-sidebar-component></live-sidebar-component>

      <div id="page-wrapper">

        <live-breadcrumbs></live-breadcrumbs>

        <div id="page">
           <router-outlet></router-outlet>
        </div>
      </div>

      <div id="promobar">
        <ticket-component></ticket-component>
      </div>
  </div>
like image 390
Ned Avatar asked Oct 03 '16 14:10

Ned


1 Answers

There are plans to bring support for canReuse.

See https://github.com/angular/angular/issues/7757#issuecomment-236737846 for more details.

As a workaround you can subscribe to params changes and do the cleanup there instead:

  constructor(public route: ActivatedRoute) {
    this.params = this.route.params.subscribe(
      params => {
        // do cleanup
        console.log(params['someParam']);
      }
    );
  }
like image 89
Günter Zöchbauer Avatar answered Oct 20 '22 04:10

Günter Zöchbauer