0
サービスからエラーを取得できません。 コンポーネントがサービスを呼び出しているため、返される可能性のあるエラーをサブスクライブします。しかし、私は呼び出すメソッドを購読することは許されていないようです。角2 - '呼び出し'コンポーネントのhttpエラーを購読できますか?
コンポーネントで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");
});
何あなたが少しあなたの方法をリファクタリングする必要があります:)
ああ、サービス内で購読するのは悪い習慣ですか?私は別の場所からの例を使っています。 https://coryrylan.com/blog/angular-2-observable-data-services サービス内で購読することが望ましいことがありますか? – Sojye
いいえ、それは悪い習慣ではありませんが、あなたがやりたいことに依存します;-)しかし、何回か購読するときは注意する必要があります。例えば、HTTPからのもののような低温観測可能なもの(共有することができない観測可能なもの)の場合、対応する要求は数回(購読した時間)実行されます。 'this._http.get( 'http:localhost/testservice')。share();'が必要です。あなたのケースでは、問題はあなたがサービスに加入し、サブスクリプションを返すことです(サブスクリプションが返すもの)。 –