Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

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: UsebootstrapApplication() to launch a standalone root component.
  • Provide features: AddprovideRouter(),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: SeeRouter,HttpClient, andServices & DI.
  • Register providers at the smallest useful scope (feature or route) to improve performance and testability.
  • UseprovideRouter() 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(): EnablesHttpClient app-wide.
  • RouterOutlet: Renders the active route's component.

Notes:

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


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: AttachesprovideHttpClient() 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

  • AddprovideHttpClient() once at the desired scope.
  • EnablesHttpClient 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, UseprovideHttpClient() over importingHttpClientModule.
  • 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 ourterms of use,cookies andprivacy policy.

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


[8]ページ先頭

©2009-2025 Movatter.jp