[英]Wait for resource to load in guard using ngrx and selectors
TLDR: I've got a isLoading
selector, I want to use that in my guard to active the route only when this selector is false. TLDR:我有一个isLoading
选择器,我想在我的警卫中使用它来仅在此选择器为 false 时激活路由。
Here is the reducer :这是减速器:
export const initialState: State = {
loaded: false,
loading: false,
success: {
[...]
}
};
export const reducer = createReducer(
initialState,
on(
fromApiActions.loadRequest,
(state): State => ({
...state,
loading: true
})
),
on(
fromApiActions.loadRequestSuccess
(state, partialState): State => ({
...state,
loaded: true,
loading: false,
success: {
...state.success,
...partialState
}
})
),
on(
fromApiActions.loadRequestFail,
(state): State => ({
...state,
loaded: false,
loading: false
})
)
);
And I've got a selector that select loading
from state.我有一个选择器,可以从状态中选择loading
。 When bootstrapping the app, I'm dispatching the LoadRequest
action which turns loading
to true.在引导应用程序时,我正在调度LoadRequest
操作,该操作将loading
变为 true。
Here is my guard :这是我的守卫:
export class CanActivateChildrenGuard implements CanActivateChild {
constructor(private store: Store<fromDataUserInformations.State>) {}
canActivateChild(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return this.store.pipe(
select(fromDataUserInformations.isLoading),
// what to use here?
);
}
}
I cannot manage to make the guard wait for the LoadRequestSuccess
to execute (which is in the LoadRequest
effect) in order to turn loading
to false
so I can return something like !loading
I guess.我无法让守卫等待LoadRequestSuccess
执行(这是在LoadRequest
效果中),以便将loading
变为false
这样我就可以返回类似!loading
东西,我猜。
Thanks!谢谢!
return this.store.pipe(
select(fromDataUserInformations.isLoading),
skipWhile(flag => !flag),
skipWhile(flag => flag),
mapTo(true),
);
it waits until fromDataUserInformations.isLoading
is true, then it waits until fromDataUserInformations.isLoading
is false again, then it releases the guard with true (means allows navigation).它等待直到fromDataUserInformations.isLoading
为真,然后等待fromDataUserInformations.isLoading
再次为假,然后用真释放守卫(意味着允许导航)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.