mercredi 26 juin 2019

Use a implementation of provider inside other implementation of same provider in Angular

I want to inject a provider implementation inside another other implementation of the same provider in Angular, but I could not achieve this. The idea is that I can specialize the second implementation with the data from first implementation As I read in angular documentation, is not possible use a second implementation of the same provider, but I want to know If someone has achive this in some way

Thanks

export abstract class Provider {
    abstract someMethod();
}

export class implProviderOne implements Provider{
    someMethod(){
    }
}

export class implProviderTwo implements Provider{
    constructor(private provider: Provider) {} // I DI injects implProviderOne in this implementation
    someMethod(){
    }
}

//This is what I try

//Main Module
//main.module.ts
@NgModule({
    ...
    imports: [InnerModule],
    providers:[
        { provide: Provider, useClass:implProviderOne}
    ], 
    ...
})
export class MainModule {}

//Inner Module
//inner.module.ts
@NgModule({
    ...
    {provide: Provider, useClass:implProviderTwo } // What I can do to achieve this?
    ...
})
export class InnerModule {}

Aucun commentaire:

Enregistrer un commentaire