Lesson 20 / 30

HttpClient Basics

Call a backend API from a service.

Enabling it

provideHttpClient() registers HttpClient so it can be injected anywhere in the app.

// main.ts
bootstrapApplication(AppComponent, {
  providers: [provideHttpClient()],
});

A GET request

HttpClient.get<T>() returns an Observable — nothing happens until something subscribes to it.

@Injectable({ providedIn: 'root' })
export class ProductService {
  private http = inject(HttpClient);

  getProducts() {
    return this.http.get<Product[]>('/api/products');
  }
}