我正在尝试将Angular4 HttpClient与我定义的可观察对象一起使用 . 不幸的是,我似乎无法将响应映射到对象 .
问题似乎是我正在使用httpclient(它隐式返回json,所以没有response.json()函数),据我所知,http被弃用了?无论如何因为这个,response.json()会导致错误;错误TypeError:response.json不是函数
代码;
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import {JsonpModule, Jsonp, Response} from '@angular/http';
export class BucketList {
constructor(public name: string,
public creationdate: string) {
}
}
@Injectable()
export class DocumentSearchService {
constructor(private _http : HttpClient) { }
getBucketList () : Observable<BucketList> {
let serviceURL = "http://localhost:3000/listBuckets";
return this._http.get(serviceURL, {withCredentials: true, responseType: 'json'})
.map((response: Response) => <BucketList>(response.json())
.catch((error: any) => window.console.log(error)));
}
}
ngOnInit() {
// this.BucketList = this.DocumentSearchService.getBucketList
this.BucketList =
this.DocumentSearchService.getBucketList().subscribe(value => {
}
有人能指出我正确的方向吗?谷歌搜索和搜索到目前为止没有答案...
谢谢 .
1 回答
responseType: 'json'
和response.json()
可以省略,因为它们是隐含的by default: