可以在这个简单的订阅上调用不带参数的方法吗?

It is possible to call a method with no parameters on this simple subscribe?

我在我的代码上使用了一个 observable,我想保持它 尽可能干净 我需要调用 detectChanges() 方法来更新 *ngIf= html.

有没有办法调用 subscribe() 上的 detectChanges() 方法,类似于下面的示例?

示例(因为过载而不工作):


this.subscriptions.add(
  this._hotelEffect
    .getHotel()
    .pipe(map(this.createHotel))
    .subscribe(this._changes.detectChanges)
);

我目前的最佳分辨率:


this.subscriptions.add(
  this._hotelEffect
    .getHotel()
    .pipe(
      map((resp) => {
        this.createHotel(resp);
        this._changes.detectChanges();
      })
    )
    .subscribe()
);

你是这个意思吗?

this.subscriptions.add(
  this._hotelEffect
    .getHotel()
    .pipe(map(this.createHotel.bind(this)))
    .subscribe(()=>this._changes.detectChanges())
);

假设this.createHotel可以消耗return类型的getHotel()

i want to keep it clean as posible and i need to call

如果真是这样,那我就这么干

  this._hotelEffect
    .getHotel()
    .subscribe((resp) => {
        this.createHotel(resp);
        this._changes.detectChanges();
      });

此外,由于您已经声明您收集订阅以在组件销毁时取消订阅 - 使用 takeUntil 运算符

有更好更简洁的方法
  //field 
  private onDestroy=new Subject();
  ngOnDestroy(){ onDestroy.next();onDestroy.complete()}
   
  //and then with ANY observable in the component
  this._hotelEffect
    .getHotel()
    .pipe(takeUntil(this.onDestroy))
    .subscribe((resp) => {
        this.createHotel(resp);
        this._changes.detectChanges();
      });