Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unsubscribe from an observable inside a route resolve class

In the ngOnDestroy method, I unsubscribe from an observable which I subscribed to once else the code is multiple times executed...

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

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

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

}

ngOnDestroy()
{
    this.sub.unsubscribe();
}

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.

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...

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

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

  constructor(private service: TasksService, private router: Router)
  {

   this.navigationEnded = this.router.events
      .filter(event => event instanceof NavigationStart)
      .map(() => this.router.routerState.root)
      .subscribe((event) =>
      {
         this.navigationEnded.unsubscribe();
      });
  }

UPDATE

When I put this code in the resolve method:

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

there is no id in the params array, its just of length 0.

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

like image 217
Pascal Avatar asked Dec 31 '16 17:12

Pascal


People also ask

Do you have to unsubscribe from observables?

In Angular applications, it's always recommended to unsubscribe the observables to gain benefits like: Avoids Memory Leaks. Aborting HTTP requests to avoid unwanted calls.

How do I unsubscribe from RxJS observable?

Unsubscribing Manually One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS provides us with a convenient method to do this. It lives on the Subscription object and is simply called . unsubscribe() .

How do you destroy observable?

There is also a better way to unsubscribe from or complete Observables by using the takeUntil() operator. The takeUntil() operator emits the values emitted by the source Observable until a notifier Observable emits a value. Let's see how to use this operator to complete Observables when the component is destroyed.


1 Answers

You can just use the first() operator. This way the observable completes after the first event. No need to unsubscribe this way:

this.router.events
  .filter(event => event instanceof NavigationStart)
  .map(() => this.router.routerState.root)
  .first()
  .subscribe((event) =>
  {
     //this.navigationEnded.unsubscribe();
  });
like image 90
Günter Zöchbauer Avatar answered Sep 17 '22 12:09

Günter Zöchbauer