简体   繁体   English

为什么在 Angular takeuntil ngUnsubscribe 的“完成”之前有一个“下一步”?

[英]Why a 'next' before 'complete' of Angular takeuntil ngUnsubscribe?

There are tons of info out there on using 'takeUntil' operator to unsubscribe from a stream, like so:有大量关于使用“takeUntil”运算符取消订阅流的信息,如下所示:

 ... export class CategoryOptionInputComponent constructor(private svc: MyService, protected router: RouterService) { ... this.router.routerUrl$.pipe(takeUntil(this.ngUnsubscribe)).subscribe(s => { const param1 = s.state.featureId; this.svc.InitFromApi(param1); }); ... } ngOnDestroy() { this.ngUnsubscribe.next();//<-- This line is confusing this.ngUnsubscribe.complete(); } private readonly ngUnsubscribe = new Subject(); }

As the above code shown, we needed to load data from API on router data being parsed, and continue to load new data when new router data arrives, until the component is destroyed.如上代码所示,我们需要在解析的路由器数据上从API加载数据,并在新的路由器数据到达时继续加载新数据,直到组件被销毁。

When a user navigating to a different route, the component is expected to be destroyed without a last hit on the API.当用户导航到不同的路线时,预计该组件将在没有最后一次点击 API 的情况下被销毁。 However it does not behave as expected.但是,它的行为并不像预期的那样。 The call to API is made even if the component is about to be destroyed.即使组件即将被销毁,也会调用 API。

My questions are:我的问题是:

(1) How to prevent the component to hit the API before it dies? (1) 如何防止组件在死之前命中API?

(2) Why not just complete() the stream, why a next() call before complete() (see the above code)? (2) 为什么不只是 complete() 流,为什么在 complete() 之前调用 next() (参见上面的代码)?

Thanks ahead.先谢谢了。

1) If you want the API call to be cancelable it needs to be part of the observable chain. 1) 如果您希望 API 调用可取消,则它需要成为可观察链的一部分。 When the call is performed inside subscribe there's no way it can be canceled so you'll have to use an operator for this such as mergeMap or concatMap .当调用在subscribe内部执行时,无法取消它,因此您必须为此使用运算符,例如mergeMapconcatMap

this.router.routerUrl$.pipe(
  takeUntil(this.ngUnsubscribe),
  concatMap(s => {
    const param1 = s.state.featureId;
    return this.svc.InitFromApi(param1);
  })
).subscribe();

However, this expects that this.svc.InitFromApi() returns an Observable or a Promise.然而,这期望this.svc.InitFromApi()返回一个 Observable 或一个 Promise。 If you subscribe inside InitFromApi() or use then() on a Promise then it makes in unsubscribable.如果您在InitFromApi()内订阅或在 Promise 上使用then()那么它会导致InitFromApi()订阅。

2) That's how takeUntil is designed. 2) 这就是takeUntil的设计方式。 It only reacts to next notifications.它只对next通知做出反应。 If you don't want to call both next() and complete() you can use defaultIfEmpty() operator:如果您不想同时调用next()complete()您可以使用defaultIfEmpty()运算符:

this.router.routerUrl$.pipe(
  takeUntil(this.ngUnsubscribe.pipe(
    defaultIfEmpty(null),
  ))
).subscribe(...);

Then you can call just this.ngUnsubscribe.complete() it'll trigger takeUntil as well.然后你可以只调用this.ngUnsubscribe.complete()它也会触发takeUntil

Actually, in your example you don't even need to call complete() if you call next() because the next notification will make the chain to dispose.实际上,在您的示例中,如果您调用next() ,您甚至不需要调用complete() ,因为下一个通知将使链进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 使用 takeUntil 进行可观察销毁:ngOnDestroy 中缺少.next() 时会发生什么 - Angular Observable destroy with takeUntil: What happens when .next() is missing in ngOnDestroy Angular:在运行下一步之前确保服务已完成 - Angular: Ensure Services is Complete before Running Next Step 将.next() 与 takeUntil 一起使用时的参数 - Argument when using .next() with takeUntil angular2 / rxjs ngUnsubscribe:如何使用Decorator取消订阅所有订阅? - angular2/rxjs ngUnsubscribe: how to unsubscribe from all subscriptions with decorator? 如何在Angular 2+中执行多个http请求,但等待每个请求完成后再执行下一个? - How can I perform multiple http request in Angular 2+ but wait for each to complete before doing the next? Angular 2打字稿调用void方法,等待该方法执行完成,然后再执行下一步 - Angular 2 typescript call a void method wait for that method executing to complete before executing next step 为什么在这个ngrx例子中有必要使用until? - Why takeUntil is necessary in this ngrx example? angular Subscription.add 方法与 takeUntil - angular Subscription.add Method vs takeUntil 在Angular中继续前进之前,等待方法完成 - Await for method to complete before moving on in Angular Angular 8 在身份验证完成之前抑制 HTTP 调用 - Angular 8 suppress HTTP calls before authentication is complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM