在 Action 文件中定义泛型类型:
import { Action } from '@ngrx/store';
export class LoadDataSuccess implements Action {
readonly type = '[Data] Load Data Success';
constructor(public payload: T) {}
}
在 Effect 文件中导入 Action 类:
import { LoadDataSuccess } from './actions';
在 Effect 中使用 ofType 操作符筛选出特定类型的 Action:
import { ofType } from '@ngrx/effects';
// ...
this.actions$.pipe(ofType(LoadDataSuccess))
在 Effect 中使用泛型类型访问 Action 中的数据:
import { map } from 'rxjs/operators';
// ...
this.actions$.pipe(
ofType(LoadDataSuccess),
map((action: LoadDataSuccess) => action.payload)
)