Angular NullInjectorError: No provider for InjectionToken errror

Discussão em 'Angular' iniciado por Sam, Dezembro 17, 2024.

  1. Sam

    Sam Guest

    I' m receiving the error NullInjectorError: No provider for InjectionToken IPublicationsRequestService! in my app when injecting my service in any component or resolver (all part of the module where the actual service is defined).

    The service should be injectable as a concrete class or as an interface using an injection token so in my lazy-loaded module I have defined :

    in publications-request.module.ts:

    providers:[
    {
    provide: PUBLICATIONS_REQUEST_SERVICE_TOKEN,
    useExisting: PublicationsRequestService
    },
    PublicationsRequestService
    ]


    However, if instead of providing this service in the lazy-loaded module where it is defined, I provide it to the root app.module of the application:

    in app.module.ts :

    providers:[
    {
    provide: PUBLICATIONS_REQUEST_SERVICE_TOKEN,
    useExisting: PublicationsRequestService
    },
    PublicationsRequestService
    ]


    then all works fine !

    Of course I dont want to define this provider at root level. So what am I missing here ? Should it not work the way I did it in my publications-request module where all components/services/resolvers are defined ?

    FYI, the service is define as :

    @Injectable({
    providedIn: 'root'
    })
    export class PublicationsRequestService extends
    BaseRepositoryService<PublicationsRequest> implements
    IPublicationsRequestService { }

    Continue reading...

Compartilhe esta Página