简体   繁体   中英

Angular 6/RxJS How to unsubcribe using takeUntil() and route params

I need to do something that I find too tricky. I'm trying to unsubscribe from a Subscription using takeUntil and waiting for the route params to emit a change.

Here is the tricky part: I need to set the takeUntil inside the route params subscribe . It should be something like this:

ngOnInit(): void {
    this.route.params.subscribe(params => {
        this.autoMethodSubscription = interval(5000).pipe(takeUntil(this.route.params)).subscribe(() => { this.autoMethod(); });
    });
}

The autoMethodSubscription does not work because it insta unsubscribes I think. Any ideas?

You can skip the first one.

interval(5000).pipe(
   takeUntil(
      this.route.params
         .pipe(skip(1))
)).subscribe(() => //... 

Or you can filter the route.params changes to specify what kind of route changes will force unsubscribing.

Or you can use the most common pattern, unsubscribe on destroy.

What exactly is your goal?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM