初始化应用程序时,我想已经加载用户(假设 localStorage 中有一个令牌)。我遵循了本指南https://www.intertech.com/Blog/angular-4-tutorial-run-code-during-app-initialization/但我现在不知道如何将它与 ngxs 结合起来,尤其是用我的设置。
该函数在应用程序初始化时调用:
initializeApp() {
const token = localStorage.getItem('token');
if (token) {
this.store.dispatch(new CheckSession(token));
//hold the user
}
}
行动
@Action(CheckSession)
checkSession(sc: StateContext<SessionStateModel>, { payload }: CheckSession) {
sc.patchState({
isLoading: true,
token: payload
});
this.sessionService.checkSession()
.subscribe(response => {
sc.dispatch(new CheckSessionSuccess(response));
}, error => {
sc.dispatch(new CheckSessionFailed(error));
});
}
我正在链接状态中的操作,我希望我的应用程序先完成整个操作链,然后再继续应用程序。
这是可能的还是我做错了?