Complete the code to import the Angular HTTP client module.
import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [[1]] }) export class AppModule {}
You need to import HttpClientModule to use HTTP services in Angular.
Complete the code to inject HttpClient into the service constructor.
import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class DataService { constructor(private [1]: HttpClient) {} }
The injected variable name is usually http or similar, but must be a valid identifier.
Fix the error in the HTTP GET request to handle errors using catchError.
this.http.get('/api/data').pipe( [1](error => { console.error('Error occurred:', error); return throwError(() => error); }) ).subscribe(data => console.log(data));
The correct RxJS operator to catch errors is catchError.
Fill both blanks to create an error handling function that logs and rethrows the error.
import { throwError } from 'rxjs'; import { HttpErrorResponse } from '@angular/common/http'; handleError(error: [1]) { console.error('An error happened:', error.message); return [2](() => error); }
The error parameter type is HttpErrorResponse and throwError is used to rethrow the error as an observable.
Fill all three blanks to use the error handler in the HTTP request with pipe and catchError.
this.http.get('/api/items').pipe( [1](this.[2]), [3](this.handleError) ).subscribe({ next: data => console.log(data), error: err => console.log('Handled error:', err) });
retry instead of pipe to chain operators.pipe inside pipe.catchError to handle errors.Use pipe to chain operators, tap for side effects, and catchError to handle errors.