如何从效果中的 ngrx 存储中检索值
我是 Angular 的 nrgx 新手。
我正在尝试从 Effect.ts 中的商店检索值 我创建了自定义路由器,将其保存在商店中,并且还创建了名为 getRouterState 的选择器。
对于这个效果我应该怎么做呢?
login$ = createEffect(() =>
this.actions$.pipe(
ofType(AuthActions.login),
switchMap( () => combineLatest([this.authService.initializeLogin()])),
switchMap( ([profile]) => {
if (profile)
{
return of(loginSuccess({ userProfile:profile, isLoggedIn:true }));
}
return of(logoutSuccess());
}),
tap((interaction) => {
if(interaction.type === AuthActionEnum.LogoutSuccess) {
this.router.navigateByUrl('/unauthenticated');
}
if(interaction.type === AuthActionEnum.LoginSuccess) {
//Here I need to read the store, this give always route.url = undefined
this.store.select(getRouterState).subscribe(route =>
{
//console.log(route.url);
this.router.navigateByUrl(route.url);
});
}
})));
I am newbie in nrgx in angular.
I am trying to retrieve a value from my store in Effect.ts
I create my custom Router, save it in the store, and I also create my selector called getRouterState.
How should I do it on this effect?
login$ = createEffect(() =>
this.actions$.pipe(
ofType(AuthActions.login),
switchMap( () => combineLatest([this.authService.initializeLogin()])),
switchMap( ([profile]) => {
if (profile)
{
return of(loginSuccess({ userProfile:profile, isLoggedIn:true }));
}
return of(logoutSuccess());
}),
tap((interaction) => {
if(interaction.type === AuthActionEnum.LogoutSuccess) {
this.router.navigateByUrl('/unauthenticated');
}
if(interaction.type === AuthActionEnum.LoginSuccess) {
//Here I need to read the store, this give always route.url = undefined
this.store.select(getRouterState).subscribe(route =>
{
//console.log(route.url);
this.router.navigateByUrl(route.url);
});
}
})));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
我鼓励您将该效果拆分为多个较小的效果。
/unauthenticated
concatLatestFrom(() => this.store 读取存储。 store.select(getRouterState))
,然后导航到从商店检索的 urlI would encourage you to split that effect out into multiple smaller effects.
/unauthenticated
concatLatestFrom(() => this.store.select(getRouterState))
, and then navigates to the url retrieved from the store