【问题标题】:Rxjs error with pipe of operators操作员管道的 Rxjs 错误
【发布时间】:2017-11-30 02:12:43
【问题描述】:

switchMap 运算符有错误:

@Injectable()
export class AvailableStoreTypesLoadedEffect {
constructor(private actions$: Actions,
          private service: AvailableService) {

}

 @Effect()
 AvailableStoreTypesLoadedEffect$ = this.actions$
 .ofType(FETCH_AVAILABLE_STORE_TYPES)
 .pipe(
   switchMap(action => this.service.fetchAvailableStoreTypes()),//Error:(22, 9) TS2684:The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<{}>'.
   map(res => new AvailableStoreTypesLoaded(res))
  );
}

我试过了:

Observable.of({})
  .pipe(
    switchMap(() => Observable.of({}))//Error:(22, 9) TS2684:The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<{}>'.
  );

但我得到了同样的错误。

我的环境是:

  • 角度5.0.3
  • 打字稿 2.4.2
  • rxjs 5.5.2

【问题讨论】:

  • 你的this.service.fetchAvailableStoreTypes() 是否返回了一个 observable?
  • 是的,我回return Observable.of({});
  • 我试过了:Observable.of({}) .pipe( switchMap(() =&gt; Observable.of({})) );,我得到了同样的错误
  • 如果您可以发布比这更多的代码,我认为这对您理解您的问题非常有帮助。查看pipe 被调用的内容、fetchAvailableStoryTypes 的外观以及AvailableStoryStypesLoaded 的外观将特别有帮助。
  • 我用更多数据编辑了帖子。

标签: angular rxjs


【解决方案1】:

我从import { mergeMap } from 'rxjs/operator/mergeMap'; 导入了库,但它必须从import { mergeMap } from 'rxjs/operators'; 导入

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多