typescript - rxJs のメソッド ,combinelatest を非推奨ではないメソッドに置き換える方法は?

okwaves2024-01-25  8

combinlatest rsjx/operator one を使用してメソッドを実装しました。正常に動作しますが、ソナーの問題により非推奨と表示されたため、最新のものに変換する必要があります。しかし、試してみると、エラーが発生したインポートを置き換えるだけです。これを行うには専門家の助けが必要です。

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 }));
        })
      )
    )));


------------------------

rxjs/oeprators ではなく rxjs からインポートし、次のように使用する必要があります。

import { combineLatest } from 'rxjs';

combineLatest([
  this.actions$.pipe(ofType(ActionType.A)),
  this.service.getoc()
]).pipe(mergeMap(...));

1

うわー、そうだったんだ! Angular では、rxjs または rxjs/operators のいずれかをインポートできます。一方は非推奨になり、もう一方は非推奨になります。

– ベン・ピーターセン

2021 年 3 月 19 日 20:17



------------------------

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
      })
    ))
  )
)

5

これを試してみます。実際に使用されるコードは ''rxjs/operators'; です。 - combinlatest を使用すると、非推奨だった sonarissue が表示されます。ありがとうございます。

– うま

2020 年 9 月 3 日 10:36

2

@Rafi Henig rxjs の combinlatest と rxjs/operators には違いがあります。演算子 1 は確かに非推奨です。

– モクシマナガルム

2020 年 9 月 3 日 10:41

1

withlatestFrom は、combinelate とは異なる動作になります。

– フリド

2020 年 9 月 3 日 10:55

1

@fridoo はソースを使用する必要があり、2 番目の Observable がすでに 1 回発行されている場合、withLatastFrom を使用するのが正しい選択でした。そのコンテキストでは起こらないようなので、技術的にはあなたの意見は正しいです。

– ラフィ・ヘニグ

2020 年 9 月 3 日 11:03

withlatestfrom を試しましたが、この状況では機能しません。ただし、スイッチマップを機能させるには、 ngOnIniteffect 内でこのエフェクトを呼び出します。

– うま

2020 年 9 月 3 日、11:12

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。