我有一个 Web API 返回一些结构化数据。
{
i: {
f: [...],
a: val,
b: val
},
a: [...],
b: [...],
c: [...]
}
我将其返回到 Angular 6 应用程序 - 我假设使用 Observables 和 rxjs。我看到的所有可观察值的示例都假设一个数组被转换为可观察值。但是,该数据是结构化的,并且在结构中包含一些子数组。
我是 Angular 的新手,所以如果可能的话,我正在寻找一些方向。
最佳答案
服务:
import {
HttpClient,
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpHeaders
} from '@angular/common/http';
constructor(private httpClient: HttpClient,
) {
}
/**
* This method is use for send GET http Request to API.
* @param url - Additional request URL.
* @param options - Header(s) which will pass with particular request.
*/
get(url: string, options?: any): Observable<any> {
return this.httpClient.get(url, this.requestOptions(options))
}
/**
* Request options.
* @param headerOptions
* @returns {RequestOptionsArgs}
*/
private requestOptions(headerOptions?: any): any {
options = {
headers: new HttpHeaders({
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
})
}
}
组件.ts:
myData: Array<any>;
/**
* This method is used to get data
*/
getFormData(filter) {
this.requestURL = `your_url`;
this.http.get(this.requestURL, options).subscribe(response => {
this.myData = response as ArrayCast[]
console.log(response)
})
;
}
export class ArrayCast{
a: any;
b: any;
c: any;
}
关于angular - 如何将结构化数据转化为可观察数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393609/