我在Angular 4中有以下代码:
constructor(private baseUrl: string, private http: Http) {}
get(url: string, config?: RequestOptionsArgs): Promise<any> {
return this.http.get(this.prependBaseUrl(url), config)
.toPromise();
}
我想将其重写为HttpClient而不是Http,所以我修改了向构造函数注入的http类型,但我不知道(并且在任何地方都找不到)与config?: RequestOptionsArgs
的关系。
感谢帮助
答案 0 :(得分:1)
尝试基本方法:
导入HttpClientModule
并将其添加到imports[]
中的app.module.ts中
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],
declarations: [
AppComponent,
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
app.service.ts
将HttpClient
导入组件
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class AppService{
constructor(private http: HttpClient) { }
getData(): Observable<any>{
return this.httpClient.get('request_url',RequestOptionsArgs)
}
}
app.component.ts:
import {AppService} from './app.service.ts';
constructor(private appservice: AppService){
this.appservice.getData().subscribe(response => {
console.log(response)
})