具有通用参数结构的NGRX操作

问题描述 投票:0回答:1

在 Angular 中,我试图为 NgRX 效果提供一个共享的错误处理机制。我想要一个所有错误

Action
都会实现的通用接口。例如:

export interface ErrorProperties { error: string };

const createFailed = createAction('[CreateFailed]', props<ErrorProperties>());
const deleteFailed = createAction('[DeleteFailed]', props<ErrorProperties>());

我希望能够在一个

Effect
中使用所有这些,如下所示:

const errorActions: ? = [createFailed, deleteFailed];

handleError = createEffect(() =>
  this._actions.pipe(
    ofType(...errorActions),
    tap((action) => console.log(action.error));
  )
);

我希望效果具有类型安全性,并认识到

action
保证具有
error
属性,但我不确定
errorActions
应该具有什么类型。

angular typescript redux ngrx
1个回答
0
投票

为了让这个问题有一个正确的答案,我将在下面再次写下解决方案:

export interface ErrorProperties { error: string };

const createFailed = createAction('[CreateFailed]', props<ErrorProperties>());
const deleteFailed = createAction('[DeleteFailed]', props<ErrorProperties>());

效果:

const errorActions = [createFailed, deleteFailed];

handleError = createEffect(() =>
  this._actions$.pipe(
    ofType(...errorActions),
    tap(action => console.log(action.error))
  )
)
© www.soinside.com 2019 - 2024. All rights reserved.