将 .next() 与 takeUntil 一起使用时的参数

And*_*ard 9 rxjs angular

我最近注意到在升级我的 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();
  }

}
Run Code Online (Sandbox Code Playgroud)

但现在你必须向它发送一个参数,如下所示:

this.ngUnsubscribe$.next(null);
Run Code Online (Sandbox Code Playgroud)

或者

this.ngUnsubscribe$.next(true);
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么?您知道要发送什么值?

eko*_*eko 17

将 rxjs 版本从 6 升级到 7 后会发生这种情况

Rxjs 7 变化

在检查了变更日志和关于这种情况的几个 github issues 后,

主题:解决主题构造函数错误地允许参数的问题 (#5476) (e1d35dc)

主题:无默认通用 (e678e81)

变更日志 7.0.0-beta.1从测试中删除空值的提交

在此输入图像描述

我意识到解决方案是要么提供一个值,要么简单地对 with 进行类型转换Subject<void>正如@martin也所说),就像destroy$ = new Subject<void>()你想用next一个空值一样。

我的相关帖子:rxjs 7 update - 主题 - 预期 1 个参数,但得到 0 个


mar*_*tin 11

您将您的主题定义为Subject<void>。调用next()想要发出undefined

所以你应该打电话this.ngUnsubscribe$.next(void 0)