1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Injection Token Renamed When Importing Angular Library

Discussão em 'Angular' iniciado por Usche, Novembro 5, 2024 às 11:52.

  1. Usche

    Usche Guest

    I have created a test library (NgxTest) using ng-packagr, which makes use of the APP_BOOTSTRAP_LISTENER injection token:

    import { APP_BOOTSTRAP_LISTENER, ComponentRef, NgModule } from "@angular/core";

    @NgModule()
    export class NgxTest {
    static forRoot() {
    console.log(APP_BOOTSTRAP_LISTENER)
    return {
    ngModule: NgxTest,
    providers: [{
    provide: APP_BOOTSTRAP_LISTENER,
    useFactory: async (c: ComponentRef<any>) => {},
    multi: true,
    }]
    };
    }
    }


    However, I'm encountering an issue when I import this library into an Angular project.

    In my project, I load the library’s configuration using importProvidersFrom(NgxTest.forRoot()) in my app.config.ts:

    import { ApplicationConfig, importProvidersFrom, provideZoneChangeDetection } from '@angular/core';
    import { provideRouter } from '@angular/router';

    import { routes } from './app.routes';
    import { NgxTest } from 'ngx-test';

    export const appConfig: ApplicationConfig = {
    providers: [
    provideZoneChangeDetection({ eventCoalescing: true }),
    provideRouter(routes),
    importProvidersFrom(NgxTest.forRoot()),
    ]
    };



    For some reason, when the library module is imported this way, the APP_BOOTSTRAP_LISTENER injection token gets renamed from InjectionToken to InjectionToken2, which causes it not to be executed during bootstrapping.

    Continue reading...

Compartilhe esta Página