How to dynamically load a component in angular via module federation?

Viewed 1667

I have an angular app where I decided to expose this component:

new ModuleFederationPlugin({
  
    // For remotes (please adjust)
    name: "app1",
    filename: "app1.js",
    exposes: {
         './Heroes': './/src/app/heroes/heroes.component.ts',
    },        

The component renders in this way:

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

@Component({
 selector: 'app-heroes',
 templateUrl: './heroes.component.html',
 styleUrls: ['./heroes.component.sass']
})
export class HeroesComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
 }
}

In my main app I'd like to do this:

<app-heroes></app-heroes>

Like I would do if the component was local, how to achieve this? In theory I would have to:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';


@NgModule({
  declarations: [
   AppComponent,
   HeroComponent, //it's not local, so?
 ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
   providers: [],
   bootstrap: [AppComponent]
})
 export class AppModule { }
0 Answers
Related