我有一个 Angular2 应用程序,它从需要身份验证的源中获取数据,因此应用程序组件的 init 事件会像这样启动 auth 令牌获取:
import { Component, OnInit } from '@angular/core';
import { NgRedux } from '@angular-redux/store';
import { rootReducer } from './app.reducer';
import * as createLogger from 'redux-logger';
import { AppActions } from './app.actions';
import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { UserEpics } from './users/user.epics';
import { ReplaySubject } from 'rxjs/ReplaySubject';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(
private ngRedux: NgRedux<any>,
private actions: AppActions,
private userEpics: UserEpics,
) {
ngRedux.configureStore(
rootReducer,
{ user: { auth: { token$: new ReplaySubject(1) } } },
[
createLogger(),
createEpicMiddleware(combineEpics(...userEpics.epics)),
]);
}
ngOnInit() {
this.ngRedux.dispatch(this.actions.getAuthToken());
}
}
Redux 正在存储一个 Rxjs5 ReplaySubject,并且这个主题将从 reducer 获取令牌值,如下所示:
case AppActions.GET_AUTH_TOKEN_DONE:
user.auth.token$.next(action.token);
return Object.assign({}, user, {
auth: {
isFetching: false,
token$: user.auth.token$,
reason: undefined
}
});
这段代码对我有用,但它是否违反了 redux 商店的不可变状态原则?