首页 > 解决方案 > 如何在操作文件中使用 entityActionFactory 来自定义 ngrx/data 中的操作?

问题描述

我看到我们可以在ngrx/data中自定义我们的操作,如下所示

https://ngrx.io/guide/data/entity-actions#tagging-the-entityaction

在此处输入图像描述

因此,我的问题是如何在正常的***.actions.ts文件中创建这些自定义操作(示例

import { createAction, props } from '@ngrx/store';

export const normalFooAction = createAction(
  ‘[Foo] foo’,
  props<{ foo: string }>()
);

¿¿¿
    export const customizedNRGXDataAction = createAction(
    ———
    this.entityActionFactory.create<Hero>(
      'Hero',
      EntityOp.QUERY_ALL,
      null,
      { tag: 'Load Heroes On Start' }
    );
    ———
???

通过我之前写的entityActionFactory然后在效果中使用它?示例

    @Injectable()
        export class FooEffects {
         ***
¿¿¿
          loadCustomizedNRGXDataAction$ = createEffect(() => this.actions$.pipe(
            ofType(FooActions.customizedNGRXDataAction),
???
           ***
             ))
            )
          );

可能吗?

提前谢谢了

标签: angularreduxngrxangular-ngrx-datangrx-data

解决方案


您可以将其创建为商店中的对象,然后通过useValue模块化服务将其提供到您的模块中。

export const myEntityActionFactory = new EntityActionFactory();
// to avoid duplicates you need to assign it to window.myEntityActionFactory and create a getter function.

// use it somewhere.
@NgModule({
  imports: [
    EntityDataModule.forRoot(...),
  ],
  provides: [
    {
      provide: EntityActionFactory,
      useValue: myEntityActionFactory,
    },
  ],
})

推荐阅读