Angular 4, convert http response observable to object observable

后端 未结 3 630
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 01:20

I\'m new to the concepts of observables and need some help with a conversion.
I have a service which returns an Observable from a Http request,

3条回答
  •  野的像风
    2021-02-02 01:34

    In the Angular 4+, It can be done automatically. You can change your getPriceTags method:

    export class PriceTagService {
        constructor(private http: HttpClient) {}
    
        getPriceTags(): Observable {
    
            // 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});
    
        }
    }
    

    And your DataSource class can be:

    export class PriceTagDataSource extends DataSource {
        constructor (private priceTagService: PriceTagService) {
            super();
        }
    
        connect(): Observable {
            // Here you can retrieve the Observable from service and return directly
            return this.priceTagService.getPriceTags();
        }
    
        disconnect() {}
    }
    

提交回复
热议问题