mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
06d70a25ea
While having both `HttpClientModule` & `HttpClientTestingModule` serves no real purpose (`HttpClientTestingModule` imports `HttpClientModule`), some code bases can have those 2 together and the migration can be quite breaking. fixes #58536 PR Close #58777
Replace Http modules from @angular/common/http with provider functions
HttpClientModule, HttpClientXsrfModule, HttpClientJsonpModule are deprecated in favor of provideHttpClient and its options.
HttpClientTestingModule is deprecated in favor or provideHttpClientTesting()
This migration updates any @NgModule, @Component, @Directive that imports those modules.
Http Modules
Before
import { HttpClientModule, HttpClientJsonpModule, HttpClientXsrfModule } from '@angular/common/http';
@NgModule({
imports: [CommonModule, HttpClientModule, HttpClientJsonpModule, HttpClientXsrfModule]
})
export class AppModule {}
After
import { provideHttpClient, withJsonpSupport, withXsrfConfiguration } from '@angular/common/http';
@NgModule({
imports: [CommonModule],
providers: [provideHttpClient(withJsonpSupport(), withXsrfConfiguration())]
})
export class AppModule {}
Testing
Before
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
})
})
})
After
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
})
})
})
Before
import { HttpClientTesting } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
imports: [HttpClientTesting],
})
})
});
After
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
providers: [provideHttpClient(withInterceptorsFromDi())]
})
})
})