What is Angular platformBrowserDynamic?
`platformBrowserDynamic` is a crucial function in Angular that provides the necessary environment to bootstrap an Angular application in the browser, particularly when using Just-in-Time (JIT) compilation.
What is `platformBrowserDynamic`?
In Angular, applications need to be bootstrapped (started) within a specific platform context. platformBrowserDynamic creates an instance of a platform that is suitable for running Angular applications in a web browser using dynamic (JIT) compilation.
It belongs to the @angular/platform-browser-dynamic package, which is specifically designed for scenarios where the Angular application needs to be compiled in the browser at runtime, rather than during the build process (which is the case with Ahead-of-Time, or AOT, compilation).
When do you use it?
platformBrowserDynamic is primarily used in the application's entry point, typically main.ts, to bootstrap the root AppModule of your application. It instructs Angular how to compile and launch your application.
While modern Angular applications predominantly use AOT compilation during production builds, platformBrowserDynamic is still used for development builds, where JIT compilation is often employed for faster rebuilds and easier debugging.
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
In the example above, platformBrowserDynamic() creates the platform, and then .bootstrapModule(AppModule) tells this platform to load and run your AppModule.
JIT vs. AOT Compilation
The choice between platformBrowserDynamic and platformBrowser (used for AOT) directly relates to Angular's compilation strategy:
- Just-in-Time (JIT) Compilation: With JIT, the Angular application code is compiled by the browser at runtime. This is typically used during development.
platformBrowserDynamicis essential for JIT compilation. - Ahead-of-Time (AOT) Compilation: With AOT, the Angular application code is compiled during the build phase (before the browser loads it). This results in smaller bundle sizes, faster startup times, and better performance in production. For AOT compiled applications, you would typically use
platformBrowserfrom@angular/platform-browserinstead ofplatformBrowserDynamic.
The `PlatformRef` Interface
The platformBrowserDynamic() function returns a PlatformRef object. This object represents the entire Angular platform that is bootstrapped in the browser. It provides methods to bootstrap modules and perform other platform-level operations.
The most commonly used method on the PlatformRef object is bootstrapModule(), which takes the root NgModule (e.g., AppModule) as an argument and starts the application.
Summary
platformBrowserDynamic is the entry point for bootstrapping Angular applications in a browser environment when JIT compilation is desired, particularly during development. It's a key piece of infrastructure that bridges your application code with the browser's runtime capabilities to bring your Angular app to life.