【发布时间】:2021-10-03 04:19:13
【问题描述】:
我最近在升级我的 rxjs 版本后注意到,你不能像下面那样使用 .next() 方法 this.ngUnsubscribe$.next();:
export class TakeUntilComponent implements OnDestroy {
// Our magical observable that will be passed to takeUntil()
private readonly ngUnsubscribe$: Subject<void> = new Subject<void>();
// Our subject that we will subscribe to
subjectA$: Subject<number> = new Subject<number>();
constructor() {
this.subjectA$
.pipe(takeUntil(this.ngUnsubscribe$))
.subscribe(value => {
// logic goes here ...
});
}
ngOnDestroy(){
// Emit a value so that takeUntil will handle the closing of our subscriptions;
this.ngUnsubscribe$.next();
// Unsubscribe from our unsubscriber to avoid creating a memory leak
this.ngUnsubscribe$.unsubscribe();
}
}
但是现在你必须像这样向它发送一个参数:
this.ngUnsubscribe$.next(null);
或
this.ngUnsubscribe$.next(true);
我的问题是为什么?您知道要发送什么值?
【问题讨论】:
-
this.ngUnsubscribe$.next();会给你一个错误吗? -
你使用的是哪个版本的 RxJS?对我来说,它仍然适用于 6.6.0 版
-
它也应该适用于 rxjs 7 github.com/ReactiveX/rxjs/issues/6324
-
啊,我正在使用“rxjs”:“^7.2.0”。我得到的错误是:预期有 1 个参数,但得到了 0.ts(2554) Subject.d.ts(31, 10):未提供“值”的参数。 (方法) Subject
.next(value: any): void 没有可用的快速修复 -
为什么显示
Subject<any>而不是Subject<void>?