Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Angular App Bootstrap & Providers


Bootstrap starts your app with a standalone root component and registers providers (Router, HttpClient, etc.) at the right scope for performance and testability.


App Bootstrap Essentials

  • Start: Use bootstrapApplication() to launch a standalone root component.
  • Provide features: Add provideRouter(), provideHttpClient(), etc. at bootstrap.
  • DI: A provider tells dependency injection how to create or supply a value for a token.
  • Scope: Register at the smallest useful scope (feature/route) for performance and testability.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';

bootstrapApplication(App, {
  providers: [
    provideRouter(routes),
    provideHttpClient()
  ]
});

Notes:

  • Related: See Router, HttpClient, and Services & DI.
  • Register providers at the smallest useful scope (feature or route) to improve performance and testability.
  • Use provideRouter() with lazy routes for faster startup.

Basic Bootstrap and Global Providers

  • Bootstrap with Router and HttpClient for app-wide availability.
  • Keep the root component minimal; configure providers at bootstrap.
bootstrapApplication(App, { providers: [ provideRouter(routes), provideHttpClient() ] });

Example

import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { Routes, provideRouter, RouterOutlet } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';

@Component({
  selector: 'home-page',
  standalone: true,
  template: `<p>Home works</p>`
})
class Home {}

const routes: Routes = [
  { path: '', component: Home }
];

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet],
  template: `<router-outlet></router-outlet>`
})
class App {}

bootstrapApplication(App, {
  providers: [
    provideRouter(routes),
    provideHttpClient()
  ]
});
<app-root></app-root>

Run Example »

Example explained

  • bootstrapApplication(App): Starts the app with a standalone root component.
  • provideRouter(routes): Registers the Router and routes.
  • provideHttpClient(): Enables HttpClient app-wide.
  • RouterOutlet: Renders the active route's component.

Notes:

  • Root stays light: Keep the root component minimal; configure providers in bootstrapApplication().
  • Use functions: Use provideRouter() and provideHttpClient() instead of legacy modules.
  • Lazy first: Favor lazy routes to reduce initial bundle and speed up startup.

REMOVE ADS


Feature-Scoped Providers

  • Provide services only where needed to avoid unnecessary globals.
  • Feature/route providers can improve startup and testability.
const routes = [
  { path: 'admin', providers: [provideHttpClient()], loadComponent: () => import('./admin').then(m => m.Admin) }
];
bootstrapApplication(App, { providers: [provideRouter(routes)] });

Example

import { Routes, provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';

const routes: Routes = [
  {
    path: 'admin',
    providers: [provideHttpClient()],
    loadComponent: () => import('./admin.component').then(m => m.AdminComponent)
  }
];

bootstrapApplication(App, { providers: [provideRouter(routes)] });

Only the admin area gets the extra providers.

The rest of the app stays lean.

Example explained

  • providers on route: Attaches provideHttpClient() only to /admin.
  • loadComponent: Lazy loads the admin component on navigation.
  • Scope: Other routes do not receive these providers, keeping the root light.

Notes:

  • Scope carefully: Add providers only to features that need them to avoid unnecessary global singletons.
  • Avoid duplication: Be aware that scoping providers can create new instances; ensure this is intended.

HttpClient Setup

  • Add provideHttpClient() once at the desired scope.
  • Enables HttpClient for that scope; add interceptors as needed.
  • Keep interceptors small and focused.
bootstrapApplication(App, { providers: [provideHttpClient()] });

Example

import { provideHttpClient } from '@angular/common/http';

bootstrapApplication(App, {
  providers: [provideHttpClient()]
});

Notes:

  • Standalone-friendly: In standalone apps, Use provideHttpClient() over importing HttpClientModule.
  • Cross-cutting: Use interceptors for auth/logging; register them once at the appropriate scope.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.