在使用此方法之前,我正在练习从 json 文件中读取数据,该文件包含国家和州的列表,但我想使用 httpClient 并且通过测试我有这些方法,但是读取此数据的正确方法是什么. 早期方法:
getCountries(): Observable<Country[]> {
return of(countries['countries']);
}
getStates(countryID): Observable<State[]> {
return of(countries['states'].filter(f => f.countryId === countryID));
}
Run Code Online (Sandbox Code Playgroud)
这是一种如何工作的方法:
getC(): Observable<Country[]> {
return this.http.get<Country[]>(this.link).pipe(map(k => {
return k['countries'];
}));
}
getS(CID): Observable<State[]> {
return this.http.get<State[]>(this.link).pipe(map(v => {
return v['states'].filter(f => f.countryId === CID);
}));
}
Run Code Online (Sandbox Code Playgroud)
我看到类似的东西,但我有错误
getC3(): Observable<Country[]> {
return this.http.get<Country[]>(this.link)
.toPromise()
.then(res => <Country[]>res.countries)
.then(data => {return data});
}
Run Code Online (Sandbox Code Playgroud)
正确的方法是如何做到这一点,错误:
已经几天了,好像问题比较严重,所以问这边,请问这个json获取数据的方法是什么:
[
{
"data": [
{
"label": "Select State",
"value": null
},
{
"label": "Adeje", …Run Code Online (Sandbox Code Playgroud)