•  


Setting up HttpClient ? Angular
In-depth Guides
HTTP Client

Setting up HttpClient

Before you can use HttpClient in your app, you must configure it using dependency injection .

Providing HttpClient through dependency injection

HttpClient is provided using the provideHttpClient helper function, which most apps include in the application providers in app.config.ts .

      
export const appConfig : ApplicationConfig = {
providers : [
provideHttpClient (),
]
};

If your app is using NgModule-based bootstrap instead, you can include provideHttpClient in the providers of your app's NgModule:

      
@NgModule ({
providers : [
provideHttpClient (),
],
// ... other application configuration
})
export class AppModule {}

You can then inject the HttpClient service as a dependency of your components, services, or other classes:

      
@Injectable ({ providedIn : 'root' })
export class ConfigService {
constructor ( private http: HttpClient ) {
// This service can now make HTTP requests via `this.http`.
}
}

Configuring features of HttpClient

provideHttpClient accepts a list of optional feature configurations, to enable or configure the behavior of different aspects of the client. This section details the optional features and their usages.

withFetch

      
export const appConfig : ApplicationConfig = {
providers : [
provideHttpClient (
withFetch (),
),
]
};

By default, HttpClient uses the XMLHttpRequest API to make requests. The withFetch feature switches the client to use the fetch API instead.

fetch is a more modern API and is available in a few environments where XMLHttpRequest is not supported. It does have a few limitations, such as not producing upload progress events.

withInterceptors(...)

withInterceptors configures the set of interceptor functions which will process requests made through HttpClient . See the interceptor guide for more information.

withInterceptorsFromDi()

withInterceptorsFromDi includes the older style of class-based interceptors in the HttpClient configuration. See the interceptor guide for more information.

HELPFUL: Functional interceptors (through withInterceptors ) have more predictable ordering and we recommend them over DI-based interceptors.

withRequestsMadeViaParent()

By default, when you configure HttpClient using provideHttpClient within a given injector, this configuration overrides any configuration for HttpClient which may be present in the parent injector.

When you add withRequestsMadeViaParent() , HttpClient is configured to instead pass requests up to the HttpClient instance in the parent injector, once they've passed through any configured interceptors at this level. This is useful if you want to add interceptors in a child injector, while still sending the request through the parent injector's interceptors as well.

CRITICAL: You must configure an instance of HttpClient above the current injector, or this option is not valid and you'll get a runtime error when you try to use it.

withJsonpSupport()

Including withJsonpSupport enables the .jsonp() method on HttpClient , which makes a GET request via the JSONP convention for cross-domain loading of data.

HELPFUL: Prefer using CORS to make cross-domain requests instead of JSONP when possible.

withXsrfConfiguration(...)

Including this option allows for customization of HttpClient 's built-in XSRF security functionality. See the security guide for more information.

withNoXsrfProtection()

Including this option disables HttpClient 's built-in XSRF security functionality. See the security guide for more information.

HttpClientModule -based configuration

Some applications may configure HttpClient using the older API based on NgModules.

This table lists the NgModules available from @angular/common/http and how they relate to the provider configuration functions above.

NgModule provideHttpClient() equivalent
HttpClientModule provideHttpClient(withInterceptorsFromDi())
HttpClientJsonpModule withJsonpSupport()
HttpClientXsrfModule.withOptions(...) withXsrfConfiguration(...)
HttpClientXsrfModule.disable() withNoXsrfProtection()

Use caution when using HttpClientModule in multiple injectors

When HttpClientModule is present in multiple injectors, the behavior of interceptors is poorly defined and depends on the exact options and provider/import ordering.

Prefer provideHttpClient for multi-injector configurations, as it has more stable behavior. See the withRequestsMadeViaParent feature above.

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본