1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Why am I getting NG203 when calling createComponent

Discussão em 'Angular' iniciado por Aleksandrs Jakovļevs, Setembro 15, 2025.

  1. I am trying to use Module Federation to load a remote component. I can see that SharedComponentRole has a correct value, however on calling container.createComponent I get the following error:

    RuntimeError: NG0203: The `EnvironmentInjector` token injection failed. `inject()` function must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.dev/errors/NG0203
    at injectInjectorOnly (core.mjs:1110:15)
    at ɵɵinject (core.mjs:1131:60)
    at Object.factory (core.mjs:19790:46)
    at core.mjs:2282:47
    at runInInjectorProfilerContext (core.mjs:884:9)
    at R3Injector.hydrate (core.mjs:2281:21)
    at R3Injector.get (core.mjs:2149:33)
    at def.getStandaloneInjector (core.mjs:19829:43)
    at createRootViewInjector (core.mjs:17942:26)


    My host component:

    import {Component, ViewContainerRef, ViewChild, ApplicationRef} from '@angular/core';
    import { loadRemoteModule } from '@angular-architects/module-federation';

    @Component({
    selector: 'remote-component-wrapper',
    standalone: true,
    template: `<div #container></div>`,
    })
    export class RemoteComponentWrapper {

    @ViewChild('container', { read: ViewContainerRef })
    container!: ViewContainerRef;

    constructor(private appRef: ApplicationRef) {}

    async ngAfterViewInit() {
    const {SharedComponent} = await loadRemoteModule({
    type: 'module',
    remoteEntry: "https://localhost/remoteEntry.js",
    exposedModule: "./SharedComponent"
    });
    this.container.createComponent(SharedComponent, { environmentInjector: this.appRef.injector });
    }

    }


    My remote component:

    import {Component} from '@angular/core';

    @Component({
    selector: 'shared-component',
    standalone: true,
    imports: [],
    template: `<h1>Angular Remote Test Component</h1>`
    })

    export class SharedComponent {

    }


    I tried to wrap the createComponent call into the runInInjectionContext but it didn't help. I suspect the issue is in the way I have set up my entire host app, because when I try to load the same remote module from a helloworld host it works.

    Continue reading...

Compartilhe esta Página