How to migrate Angular CoreModule to standalone APIs
In this article we’re going to learn how to migrate commonly used Angular CoreModule
(or any other Angular module) to standalone APIs!
Angular standalone components and APIs are the future! Angular CLI now allows us to generate new Angular applications with standalone setup out of the box, simply by using the new --standalone
flag when running ng new
command!
This works really great for new greenfield projects, but we might encounter some issues in more complex enterprise environments which often tend to abstract base setup into NgModule
s in reusable libraries which are then consumed by multiple Angular apps.
Such module might prevent us from using of the standalone setup in consumer apps because we can’t import such module directly in the new app.config.ts
and the provider workaround in form of importProvidersFrom
handles only providers which is often just not enough!
// app.config.ts
import { MyOrgCoreModule } from '@my-org/core'
// standalone app setup generated by Angular CLI with --standalone
export const appConfig: ApplicationConfig = {
providers: [
// providers ...
MyOrgCoreModule, // ⚠️ doesn't work ...
importProvidersFrom(MyOrgCoreModule), // ⚠️ often not enough, doesn't work
],
};
First, let’s talk about the…