【发布时间】:2020-06-20 21:48:58
【问题描述】:
我正在尝试将 AuthGuard 与 NGRX 一起使用,但在这条路线上我总是不确定:
当我查看 Store 时,estaAutenticado 是真的,但我无法准时,因为它是异步的?
我的后卫:
return this.store.pipe(
select(fromAuth.getEstaAutenticado ),
map(authed => {
console.log(authed) // <---- Always return undefined
if (!authed) {
this.router.navigate(['/'])
return false;
}
return true;
})
);
}
```
// Reducer
export interface State {
usuario: Usuario,
estaAutenticado: boolean,
erro: string
}
export const initialState: State = {
usuario: null,
estaAutenticado: false,
erro: ''
};
export const getUsuario = (state: State) => state.usuario;
export const getEstaAutenticado = (state: State) => state.estaAutenticado;
export const getErro = (state: State) => state.erro;
【问题讨论】:
-
尝试在地图前添加
filter(authed => typeof authed !== 'undefined') -
createReducer代码在哪里? -
@SWilko 哪一部分?
-
@PrzemyslawJanBeigert 我得到了相同的结果,动作被调度我登录成功,但我仍然收到 undefined 。
标签: angular authentication store ngrx