How to unsubscribe from an observable inside a route resolve class
<p>In the ngOnDestroy method, I unsubscribe from an observable which I subscribed to once else the code is multiple times executed...</p>

<pre><code>ngOnInit()
{
    this.sub = this.route.params.subscribe(params =&gt; 
    {
        this.projectId = +params['id'];
        this.projectStore.project = this.projectId;

        // load data when the route changes
        this._tasksService.gettasks(this.projectId).subscribe(years =&gt; { this.tasks = years.map(y =&gt; new task(y)) }); // no need to clean up the subscription

    });
    // load data when the component is initialized
    this._tasksService.gettasks(this.projectId).subscribe(years =&gt; { this.tasks = years.map(y =&gt; new task(y)) }); // no need to clean up the subscription

}

ngOnDestroy()
{
    this.sub.unsubscribe();
}
</code></pre>

<p>Now I want to put this in a router resolve class but there is no ngOnDestroy - of course - just a NavigationEnd event which I again could subscribe to.</p>

<p>That means I subscribe to a NavigationStart event (which occurs when I leave the route) in order to unsubscribe another subscription which is the route params change subscription HAHAHA...</p>

<p>I guess that's not the way to go, but Google offered nothing.</p>

<p>Anyone knows how to tackle that scenario? Or should route params change subscription really ONLY belong into a component?</p>

<pre><code>  constructor(private service: TasksService, private router: Router)
  {

   this.navigationEnded = this.router.events
      .filter(event =&gt; event instanceof NavigationStart)
      .map(() =&gt; this.router.routerState.root)
      .subscribe((event) =&gt;
      {
         this.navigationEnded.unsubscribe();
      });
  }
</code></pre>

<p>UPDATE</p>

<p>When I put this code in the <strong>resolve</strong> method:</p>

<pre><code>  this.route.params.subscribe(params =&gt; 
    {
         // reload data by the new id parameter does not work with params
         // I have to use inside here: route.params['id']
    });
</code></pre>

<p>there is no id in the params array, its just of length 0.</p>

<p>Instead, I have to use route.params['id'] inside the params subscription, but why?</p>