【发布时间】:2018-11-10 17:41:22
【问题描述】:
我对 Angular (7) 有点陌生。我正在尝试在执行 HTTP 请求时检索状态代码。这是我在服务中使用的代码:
checkIfSymbolExists() {
return this.http.get(this.url, { observe: 'response' })
.subscribe(response => {
return response.status;
});
}
我在其中一个组件的方法中使用返回值,如下所示:
onSubmit() {
console.log(this.stocks.checkIfSymbolExists());
}
我希望返回一个数字,但我有一个对象:
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}
closed: true
destination: SafeSubscriber {closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
isStopped: true
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
_parent: null
_parentSubscription: null
_parents: null
_subscriptions: null
__proto__: Subscription
当我不是简单地返回 response.status 时,我对它进行了 console.log,我确实得到了预期的 200 状态代码(一个数字,而不是一个对象)。
任何想法为什么返回response.status 的值时行为不一样,如此处所示?谢谢。
【问题讨论】:
-
你的后端 API 代码是什么?
标签: javascript angular typescript http angular7