【问题标题】:How to replace method 'combineLatest' from rxJs with a not deprecated method?如何用未弃用的方法替换 rxJs 中的方法“combineLatest”?
【发布时间】:2020-12-22 13:44:35
【问题描述】:

我使用combinlatest rsjx/operator one 实现了方法,它工作正常,但声纳问题说它已弃用.so,我需要将其转换为最新的。但我尝试,只是替换导入它给出了错误。我需要一些专家的帮助才能做到这一点。

gX$ = createEffect(() => this.actions$.pipe(
    ofType(ActionType.A),
    combineLatest(this.service.getoc()),
    mergeMap(([, oc]) => this.reviewService.findBy(oc.id,
      new Date(),
      new Date(new Date().setDate(new Date().getDate() + 1)))
      .pipe(
        mergeMap(d => {
          return of(reviewLoadSuccess({ reviews: getReviews(d) }));
        }
        ),
        catchError(error => {
          return of(reviewLoadFailure({ error: error }));
        })
      )
    )));

【问题讨论】:

    标签: typescript rxjs rxjs6 ngrx-effects combinelatest


    【解决方案1】:

    由于您似乎只需要this.service.getoc() 返回的值,因此我建议您改用switchMapTo 运算符,如下所示

     gX$ = createEffect(() => this.actions$.pipe(
       ofType(ActionType.A),
       switchMapTo(this.service.getoc()),
       mergeMap(oc => this.reviewService.findBy(oc.id,
         new Date(),
         new Date(new Date().setDate(new Date().getDate() + 1)))
         .pipe(
           mergeMap(d => {
             return of(reviewLoadSuccess({ reviews: getReviews(d) }));
           }
           ),
           catchError(error => {
             return of(reviewLoadFailure({ error: error }));
           })
         )
       )));
    

    如果您还想使用该操作,请考虑应用以下更改:

    gX$ = createEffect(() => this.actions$
      .pipe(
        ofType(ActionType.A),
        switchMap(action => this.service.getoc().pipe(
          switchMap(oc => {
            //  you have access to both action, and oc
            // ... continue running your code
          })
        ))
      )
    )
    

    【讨论】:

    • 我会试试这个,实际上,代码使用 ''rxjs/operators'; - combinelatest,它会给出声纳问题,它已被弃用。谢谢。
    • @Rafi Henig combineLatestrxjsrxjs/operators 之间存在差异。运营商一确实被弃用了。
    • withLatestFrom 导致与combineLatest 不同的行为。
    • @fridoo 不得不使用源,使用 withLatastFrom 将是正确的选择,前提是第二个 Observable 已经发出一次,在他的上下文中似乎没有发生什么,所以从技术上讲你是正确的。
    • 我试过用latestfrom,但在这种情况下,它不起作用。但是 switchmapto work.this effect call inside , ngOnIniteffect .
    【解决方案2】:

    您需要从rxjs 而不是rxjs/oeprators 导入并像这样使用它:

    import { combineLatest } from 'rxjs';
    
    combineLatest([
      this.actions$.pipe(ofType(ActionType.A)),
      this.service.getoc()
    ]).pipe(mergeMap(...));
    

    【讨论】:

    • 哇,就是这样! Angular 允许您导入 rxjs 或 rxjs/operators,一个已弃用,另一个不推荐
    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 2018-03-30
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多