1

我目前正在使用NgRx/data并且我有 2 个集合:CoursesLessons

我在他们的文档中发现了一些东西来覆盖EntityCollectionReducer

@Injectable()
export class AdditionalEntityCollectionReducerMethodsFactory {
  constructor(private entityDefinitionService: EntityDefinitionService) {}
  create<T>(entityName: string): EntityCollectionReducerMethodMap<T> {
    const definition = this.entityDefinitionService.getDefinition<T>(
      entityName
    );
    const methodsClass = new AdditionalEntityCollectionReducerMethods(
      entityName,
      definition
    );

    return methodsClass.methods;
  }
}

AdditionalEntityCollectionReducerMethods中,我重写了一些方法来添加新属性:

export class AdditionalEntityCollectionReducerMethods<T> extends EntityCollectionReducerMethods<T> {
  constructor(
    public entityName: string,
    public definition: EntityDefinition<T>
  ) {
    super(entityName, definition);
  }

  protected saveAddOne(collection: EntityCollection<T>, action: EntityAction<T>): EntityCollection<T> {
    const ec = super.saveAddOne(collection, action);
    (ec as any).saving = true;

    return ec;
  }

  protected saveAddOneSuccess(collection: EntityCollection<T>, action: EntityAction<T>): EntityCollection<T> {
    const ec = super.saveAddOneSuccess(collection, action);
    (ec as any).saving = false;

    return ec;
  }

  protected saveAddOneError(collection: EntityCollection<T>, action: EntityAction<EntityActionDataServiceError>): EntityCollection<T> {
    const ec = super.saveAddOneError(collection, action);
    (ec as any).saving = false;

    return ec;
  }
}

同样在course.module.ts中,我在entityMedatata中将此属性指定为additionalCollectionState

const entityMetadata: EntityMetadataMap = {
  Course: {
    ...
    additionalCollectionState: {
      saving: false,
    },
  }
  ...
};

AdditionalEntityCollectionReducerMethods 在app.module.ts中注册为提供者:

{
   provide: EntityCollectionReducerMethodsFactory,
   useClass: AdditionalEntityCollectionReducerMethodsFactory,
},

因此,通过这种方式,我在Courses集合中添加了一个名为save的新属性。但我的问题是,如果我在其他模块中使用saveAddOne方法,保存属性也会在这里添加,我不想要这个。

我认为发生这种情况是因为我已经在 app.module 中注册了AdditionalEntityCollectionReducerMethodsFactory 但是我尝试在course.module中注册它并进行调试,但是断点没有被击中(仅在app.module中)。我还必须提到course.module是延迟加载的。

是否有可能仅在一个特定集合中添加新属性?

4

0 回答 0