This example shows how to setup Webpack Module Federation where the shell loads an Angular standalone component by using Angular routing and by dynamically instantiates an Angular standalone component and adding it to the DOM.
This example also shows how to pass inputs to the Angular component and subscribe to its outputs.
The remote webpack module contains an Angular standalone component.
The shell app is rendered in a red colored background and the remotely loaded mfe1 app is rendered in a blue colored background.
- Go to
/code-demos/component-standalone-ng16/shell-ng16
folder and runnpm i
, followed bynpm start
. This will start the shell app on http://localhost:4200. - Go to
/code-demos/component-standalone-ng16/mfe1-ng16
folder and runnpm i
, followed bynpm start
. This will start the mfe1 app on http://localhost:4201.
To see the Angular standalone component from the mfe1 app loaded into the shell go to the shell's URL and click the Load Angular standalone component named MyStandaloneComponent from mfe1
link to load the component using routing or use the Dynamically load Angular standalone component named MyStandaloneComponent from mfe1
button to load the component by dynamically instantiating it.
The mfe1 app is an Angular 16 app that contains an Angular standalone component named MyStandaloneComponent, which represents the micro frontend that we want to expose via Webpack Module Federation.
The AppRoutingModule Angular module contains a route that loads the MyStandaloneComponent
on /my-standalone-component
.
The mfe1 app will set the input of the MyStandaloneComponent
to test input value from dev platform
and subscribe to the outputs of the component which log messages to the console when the component is loaded or destroyed. Use the Go to my-standalone-component
link to load the component and use the Go to home page
link to destroy it.
On the webpack configuration file for mfe1 app you will find the declaration of the webpack modules to expose:
exposes: {
"./my-standalone-component": "./src/app/my-standalone-component/my-standalone-component.component.ts",
},
The above defines a webpack module that is named my-standalone-component
and that is mapped to the ./src/app/my-standalone-component/my-standalone-component.component.ts file, which is where the MyStandaloneComponent
Angular standalone component is defined.
When you run the mfe1 app you will see the text MFE1 dev platform
. This is to call out the fact that the mfe1 app is not exposed in its entirety via Webpack Module Federation, only the MyStandaloneComponent
Angular standalone component is. Everything else in the mfe1 app is there only with the sole purpose of supporting the local development of the mfe1 app, more specifically, the development of the MyStandaloneComponent
Angular component.
This means that the input value test input value from dev platform
set by the dev platform is not part of the exported component, neither is the subscription of the component's outputs that log to the console when the component is loaded or destroyed.
The shell app is an Angular 16 app that dynamically instantiates an Angular standalone component exposed by the mfe1 app. You can test this in two ways:
- via the
Load Angular standalone component named MyStandaloneComponent from mfe1
link which will load the component using Angular routing. - via the
Dynamically load Angular standalone component named MyStandaloneComponent from mfe1
button which will dynamically instantiate the component and add it to the DOM.
Note
There are many ways to dynamically instantiate an Angular component, this example just shows one possible way.
Also note that the approach using Angular dynamic component loading is the same as the one used by the loadV4
method shown in the component-ng16 example. For more information inspect that example, and all the comments on the loadV4
method at /code-demos/component-ng16/shell-ng16/src/app/app.component.ts.
Note
The approaches shown here are NOT limited to Angular standalone components. The same would work for non-standalone components.
Furthermore, you can also use the Angular directive approach shown in the component-directive-ng16 example to load Angular standalone components.
The /my-standalone-component
route added to the AppRoutingModule lazy loads the MyStandaloneComponent
Angular standalone component from the mfe1 app. This route uses the loadRemoteModule
function to load the webpack module.
Once the webpack module is loaded from the remote we return the exposed Angular standalone component from the mfe1 app named MyStandaloneComponent
to the loadComponent
function, which then loads the Angular component into the DOM.
This way also sets the component input. See the line with inputText: 'Hello from the shell!'
at app-routing.module.ts.
Note
This approach is not subscribing to the component's outputs because it would require a bit of extra code to do it. You could do it like it's done in the mfe1 app dev platform which subscribes to the activate
event from the router-outlet
or a better approach would be to create a wrapper component and then create an Angular service to share the data. You can take a look at the advanced-ng16 code demo to see how to create a wrapper component.
The loadMyStandalone
at app.component.ts:
- loads the remote webpack module from the mfe1 app using the
loadRemoteModule
function from the@angular-architects/module-federation
npm package. - creates an instance of the component and attach it to the DOM using the
ViewContainerRef.createComponent
method. - uses the
ComponentRef.setInput
method to set the inputs of the component. - uses the
ComponentRef.instance
object to subscribe to the outputs of the component.
The setup of Webpack Module Federation was done using the @angular-architects/module-federation npm package, which aims to streamline the setup of Webpack Module Federation for Angular apps. For more info see Basics of @angular-architects/module-federation npm package.
Also, read the official docs at:
- the readme page for the @angular-architects/module-federation npm package
- the tutorial for the @angular-architects/module-federation plugin
For more information see:
- Getting started with standalone components
- Migrate an existing Angular project to standalone
- Dynamic component loading
- Why Migrate to Angular Standalone Components
- Angular's Future with Standalone Components
- Standalone components and APIs 1st edition: This repo contains a copy of this free book which you can get from the angular architects website.
- Sharing data between child and parent directives and components: this is useful to understand Angular inputs and outputs.
- Module Federation with Angular’s Standalone Components: specifically the section named
Alternative: Dynamic Shell
which also shows a code example similar to this one.