加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 4,将http响应observable转换为object observable

发布时间:2020-12-17 07:21:16 所属栏目:安全 来源:网络整理
导读:我是可观察的概念的新手,需要一些转换帮助. 我有一个服务,它返回一个Observable Response从一个Http请求,但我需要转换它做一个Observable PriceTag在connect方法中的DataSource上使用它. 反正有没有这样做? 这是我服务的方法: getPriceTags(): ObservableR
我是可观察的概念的新手,需要一些转换帮助.
我有一个服务,它返回一个Observable< Response>从一个Http请求,但我需要转换它做一个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"
        }
    ]
}
从角度4.3开始,这可以自动完成.

例:

export class SomeService {
    constructor(private http: HttpClient) {}  // <--- NOTE: HttpClient instead of Http

    getSome(): Observable<MyAwesomeObject> {
        return this.http.get<MyAwesomeObject>.get('myUrl');
    }
}

所以在你的情况下,这将是:

返回this.http.post< PriceTag>(Constants.WEBSERVICE_ADDRESS“/ priceTag”,{headers:headers});

再次,使用HttpClient而不是Http

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读