How should I modify the response to an HTTP request and easily access it before return it out from Observable?

前端 未结 6 475
春和景丽
春和景丽 2021-02-01 23:42

I\'m upgrading to Angular to version 5, I was using @angular/http before and now I need to update to @angular/common/http and use HttpClient

I a

6条回答
  •  野性不改
    2021-02-02 00:03

    You can create your own Observable that wraps the http.get and return your manipulated response, in this example it is the manipulatedAccountsResponse object:

    getAll(): Observable {
      return Observable.create(observer => {
        this.http.get('/accounts')
                 .subscribe((result) => {
                   const manipulatedAccountsResponse = result;
                   // do something with result.
                   manipulatedAccountsResponse.setTotal(100);
                   observer.next(manipulatedAccountsResponse);
                   // call complete if you want to close this stream (like a promise)
                   observer.complete();
                });
      });
    

    }

提交回复
热议问题