如何从效果中的 ngrx 存储中检索值

发布于 2025-01-15 15:10:06 字数 1153 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2025-01-22 15:10:06

我鼓励您将该效果拆分为多个较小的效果。

  • 第一个效果用于调度成功/注销操作
  • 第二个效果用于侦听注销并导航到 /unauthenticated
  • 第三个效果用于侦听成功,然后使用 concatLatestFrom(() => this.store 读取存储。 store.select(getRouterState)),然后导航到从商店检索的 url

I would encourage you to split that effect out into multiple smaller effects.

  • One effect to dispatch the success/logout action
  • Second effect that listens to logout and navigates to /unauthenticated
  • Third effect that listens to succes, then reads store with concatLatestFrom(() => this.store.select(getRouterState)), and then navigates to the url retrieved from the store
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文