簡體   English   中英

角度2-可以在“呼叫”組件中訂閱http錯誤嗎?

[英]Angular 2 - Possible to subscribe to http errors in the 'calling' component?

我無法從服務中檢索錯誤。 我的組件正在調用服務,我想訂閱可能返回的任何錯誤。 但是,似乎不允許我訂閱調用方法。

public getMethods(v) {
    if(v != null) {
        return this._http.get('http:localhost/testservice', {
            method: 'GET',
            headers: new Headers([
                'Accept', 'application/json',
                'Content-Type', 'application/json'
            ])
        })
        .map(res => (res.json()))
        .map((method: any) => {
            if(method) {
                let result: Array<string> = [];
                var list = method.List;

                list.forEach(v => {
                    result.push(v);
                })
                return result;
            }
        })
        .subscribe(data => {
            this._dataStore.mn= data;
            this._mnObserver.next(this._dataStore.mn);
        },
        error => {
            **// I want to retrieve this error in the 'calling' component**
            return error;
        });
    }
    else {
        console.error("Get names. V was null");
    }

在組件中:這不起作用:

this._mnService.getMethods(v), error => {
      alert("error in da house");
};

這不起作用:

this._mnService.getMethods(v).subscribe( error => {
      alert("error in da house");
});

那么,什么可行? 謝謝! :)

您需要重構一下您的方法:

public getMethods(v) {
  if(v != null) {
    return this._http.get('http:localhost/testservice', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })
    .map(res => (res.json()))
    .map((method: any) => {
        if(method) {
            let result: Array<string> = [];
            var list = method.List;

            list.forEach(v => {
                result.push(v);
            })
            return result;
        }
    });
  } else {
    console.error("Get names. V was null");
  }
}

這樣,您在訂閱可觀察項時將收到錯誤:

this._mnService.getMethods(v).subscribe((data) => {
  }, error => {
    alert("error in da house");
  });

您的代碼中有些奇怪的是您訂閱了getMethods方法。 因此,您不會為請求返回可觀察到的內容,而是返回訂閱。

如果要在響應存在時觸發_mnObserver ,則可以改用do運算符。 這是一個示例:

public getMethods(v) {
  if(v != null) {
    return this._http.get('http:localhost/testservice', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })
    .map(res => (res.json()))
    .map((method: any) => {
        if(method) {
            let result: Array<string> = [];
            var list = method.List;

            list.forEach(v => {
                result.push(v);
            })
            return result;
        }
    })
    .do(data => { // <-------
      this._dataStore.mn= data;
      this._mnObserver.next(this._dataStore.mn);
    });
  } else {
    console.error("Get names. V was null");
  }
}

暫無
暫無

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

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