【发布时间】:2018-01-31 07:45:29
【问题描述】:
我是 observables 概念的新手,需要一些转换方面的帮助。
我有一个从 Http 请求返回 Observable<Response> 的服务,但我需要将其转换为 Observable<PriceTag> 以在 connect 方法内的 DataSource 上使用它。
有没有办法做到这一点?
这是我的服务中的方法:
getPriceTags(): Observable<Response> {
// Set the request headers
const headers = new Headers({ 'Content-Type': 'application/json' });
// Returns the request observable
return this.http.post(Constants.WEBSERVICE_ADDRESS + "/priceTag", null, {headers: headers});
}
这里是 DataSource 类,我需要将它作为Observable<PriceTag> 返回:
export class PriceTagDataSource extends DataSource<PriceTag> {
constructor (private priceTagService: PriceTagService) {
super();
}
connect(): Observable<PriceTag> {
// Here I retrieve the Observable<Response> from my service
const respObs = this.priceTagService.getPriceTags();
// Now I need to return a Observable<PriceTag>
}
disconnect() {}
}
这是我请求的响应示例:
{
// This object is used to check if the query on the server was sucessful
"query": {
"sucessful": true
},
// These are my PriceTags
"tags": [
{
"id": "1",
"name": "MAIN"
},
{
"id": "2",
"name": "CARD"
}
]
}
【问题讨论】:
-
其中一个答案对您有帮助吗?
标签: angular typescript rxjs observable