【发布时间】:2017-07-22 16:51:06
【问题描述】:
我正在尝试使用 observables 执行以下操作,但我不知道最好的方法是什么:
我正在使用 http 调用登录端点。该服务将返回一个令牌。我想从 APP 内的 2 个不同点依次执行 2 个不同的操作:当 http 解析时,我需要存储提供的令牌,然后显示一条消息。我尝试了以下方法:
return this.http.get(this.config.getApi('login'), params)
.map(response => response.json())
.do((data) => {
// Store token here
// If it fails, throw error
// Tried Observable.throw here but the subscriber doesn't get it.
}).subscribe(
(data) => {
// Everything went fine
},
() => {
// Either the http request failed or
// the process of storing the token threw an error.
}
);
不幸的是,除非 http 调用失败,否则返回的 observable 的订阅者永远不会出错。我应该使用什么运算符而不是 do?
谢谢。
【问题讨论】: