我想要做的是捕获成功/失败的HTTP请求,并将它们发送到同一个流中,让订阅者负责处理所有内容。
到目前为止,我所做的是通过使用嵌套的observable来处理HTTP错误情况。成功登录将在流上转发,以便success
可以处理它。但是,当发生错误时,Object.create
永远不会处理fails
有没有办法让成功和失败的HTTP请求都在同一个流中处理,让下游用户处理失败案例?
private actions$ = new BehaviorSubject<Action>({type: actions.INIT, payload: null})
let logins = this.actions$
.filter(action => return action.type === actions.LOGIN_USER )
.do(() => this.store.dispatch({ type: actions.LOGIN_REQUEST }) )
.mergeMap(action => {
const query = Observable.defer(() => this.http.post('/auth/login', JSON.stringify(action.payload)) );
return query.catch(err => {
return Observable.create(err.json());
})
})
.share();
// Works
var success = logins.filter(res => res.success).map(res => store.dispatch({...});
// Failure cases are never dispatched
var fails = logins.filter(res => !res.success).map(res => store.dispatch({...}));
当我实施上述内容时,我最终得到EXCEPTION: TypeError: this._subscribe is not a function