如何在NGRX Effect V10中声明类型?不再使用类,因此无法推断类型

问题描述

因此,由于V10升级到NGRX,您无法再将一种Action传递给效果,因为Actions现在被声明为类中的函数。在V10中还有解决此问题的方法吗?

export const myAction = createAction(
    '[myAction]',props<{person: User}> ()
);

@Effect({ dispatch: false })
    x = this.actions$.pipe(
        ofType**<myAction>**('[myAction]'),

我无法使用人道具。您在第10版之前的位置是因为它是一类...

解决方法

请参见https://ngrx.io/guide/effects#incorporating-state

  1. myAction直接传递到ofType
  2. 该动作是根据其道具键入的,因此您只需使用action.person
@Effect({ dispatch: false })
  x = this.actions$.pipe(
    ofType(myAction),map(action => of(action.person))
  );

另外,最好使用新的createEffect语法。

Stackblitz:https://stackblitz.com/edit/so-ngrx-oftype?file=index.ts