我正在创建一个通用的 crud 服务,我想在我的 Angular 应用程序中的各种功能模块中使用它。
为了实现这一点,我需要将一个字符串值传递给提供者中的服务:一个角度模块,这样我就可以为基础服务的超级设置一个值。这可能吗?
配置
import { InjectionToken } from '@angular/core';
export const COLLECTION_REF = new InjectionToken<string>('collection');
模块
import { GenericEntityService } from '@mifowi/core/services/genericEntity.service';
import { COLLECTION_REF } from './orgModuleConfig';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
@NgModule({
....
**I want to pass in a string value of 'Organization' to the GenericEntityService**
providers: [
GenericEntityService,
{ provide: COLLECTION_REF, useValue: 'Organization' }
],
})
export class OrganizationsModule {}
通用服务
import { FirestoreService } from './firestore.service';
import { Injectable } from '@angular/core';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from '@ngrx/data';
import { Observable } from 'rxjs';
@Injectable()
export class GenericEntityService extends EntityCollectionServiceBase<any> {
constructor(
@Inject(COLLECTION_REF) private collection: string,
public firestoreService: FirestoreService,
elementsFactory: EntityCollectionServiceElementsFactory
) {
**I want to set the string below to
the value passed in providers in the module - see above**
super(collection, elementsFactory);
}
getMyEntityList(entity: string, id: string): Observable<any[]> {
return this.firestoreService.getMyEntityList(entity, id);
}
getEntityByName(entity: string, str: string): Observable<any[]> {
return this.firestoreService.colWithName$(entity, str);
}
createNewEntity(entity: string, payload: any): void {
this.firestoreService.createDoc(entity, payload);
}
updateEntity(entity: string, payload: any): void {
this.firestoreService.updateDoc(entity, payload);
}
deleteEntity(entity: string, payload: any): void {
this.firestoreService.deleteDoc(entity, payload);
}
}