在提供程序中传递字符串值,并将值传递给基本服务的超级目录

时间:2019-07-19 15:01:33

标签: angular angular-providers angular-dependency-injection

我正在创建一个通用的Crud服务,希望在整个我的角度应用程序中的各个功能模块中使用。

要实现此目的,我需要将字符串值传递给angular模块的provider:中的服务,因此我可以将值设置为基础服务的上级。这可能吗?

配置

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);
        }
    }

0 个答案:

没有答案