2
通過幾個ngrx示例(https://github.com/SekibOmazic/ngrx-auth-example)和(https://github.com/ngrx/angular2-store-example),他們都創建了對商店的訂閱,然後進行REST API調用。Angular2 ngrx/store有多次登錄嘗試
我修改了他們的代碼以返回錯誤,但訂閱仍保持機智(例如,嘗試登錄時返回404)。我可以在控制檯中看到錯誤,但我可以再次登錄。
在我的代碼中,我有一個幾乎相同的設置,但失敗的登錄響應殺死我的訂閱(盡我所知)。
我需要什麼魔法才能使4XX錯誤響應代碼不殺我的訂閱?
private actions$ = new BehaviorSubject<Action>({ type: actions.INIT, payload: null })
constructor(
public http: Http,
private store: Store<AppStore>
) {
let logins = this.actions$
.filter(action => { debug('ACTION', action); return action.type === actions.LOGIN_USER })
.do(() => { debug('DISPATCH'); this.store.dispatch({ type: actions.LOGIN_REQUEST }) })
.mergeMap(action => this.http.post('/auth/login', JSON.stringify(action.payload)))
.share();
let loginSuccess$ = logins
.filter((payload: IResponse) => payload.data.token !== null)
.map((payload: IResponse) => ({ type: actions.USER_AUTHENTICATED, payload: payload.data }));
let loginFailure$ = logins
.filter((payload: IResponse) => payload.data.token === null)
.map((payload: IResponse) => ({ type: actions.LOGIN_FAILURE, payload: payload.data }));
Observable
.merge(loginSuccess$, loginFailure$)
.subscribe((action: Action) => this.store.dispatch(action))
}
login(payload: ILogin) {
this.actions$.next({ type: actions.LOGIN_USER, payload });
}
有太多的缺點,以這種方式。請看看這個:http://codereview.stackexchange.com/questions/141969/designing-redux-state-tree/142382#142382 –
@StavAlfi - 此鏈接不是最新的。你能更新嗎? – Gambo
當時,我在github上問了同樣的問題。 https://github.com/ngrx/effects/issues/52 –