簡體   English   中英

Angular2 http.get鏈接

[英]Angular2 http.get chaining

我的服務中包含以下http.get鏈:

constructor(private http:Http) {}

getDetails(sysID:string){

var details;
this.http.get('https://blahURL').map(res => res.json().filter(f => f.id == another.id)[0]).subscribe(
    (data) => details = data,             // Reach here if res.status >= 200 && <= 299
    (err) => console.log(err),            // Reach here if fails
    function(){                           // On completion

      console.log(this);

      this.http.get(...) //<- fails

在“竣工”的功能被觸發,但this我在控制台得到的是SafeSubscriber對象,所以下一個HTTP呼吁this.http失敗。

我做錯了什么?

使用粗箭頭語法保留this值。

更換

function(){  // On completion

  console.log(this);

  this.http.get(...) //<- fails

()=>{ // On completion

  console.log(this);

  this.http.get(...) 

訂閱功能中的第二個this不再引用您的原始對象。

嘗試在初始http調用之前添加對其的引用,例如:

let self = this;

並在您的訂閱回調/錯誤函數中將其用作:

self.http.get(..)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM