【问题标题】:How to routing in another component when use canActivate()使用 canActivate() 时如何在另一个组件中路由
【发布时间】:2018-12-25 08:44:20
【问题描述】:

当我使用 canActivate(); 时,我想直接在组件ResetPassIdComponent 中导航;现在,当我单击此组件 ResetPassIdComponent 时,我的应用程序首先在 /outsidelogin/login 中导航,然后在 resetPasswordRequest/:id 中导航

export class AuthGuard implements CanActivate {
    constructor(private router: Router, private auth: LoginService) { }
    canActivate(): boolean {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        this.router.navigate(['/outsidelogin/login']);
        return false;
    }
}

所以在这个 canActivate() 中我有 2 个条件,第一个如果我登录,第二个如果我没有登录导航 this.router.navigate(['/outsidelogin/login']);

我的路由.ts

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: 'fp', component: FirstPageComponent
      }
    ]
  },
  {
    path: 'outsidelogin',
    component: outsideloginComponent,
    children: [
      { path: 'login', component: LoginFirstComponent },
      { path: 'register', component: RegisterComponent },
    ]
  },
    { path: '', redirectTo: '/home/fp', pathMatch: 'full' },
    { path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
];

拜托,你能告诉我任何想法,如何解决我的问题吗?

【问题讨论】:

  • 您能否与我分享 (stackblitz.com) 链接,因为您没有正确理解您的观点。
  • 我演示了这个演示 play.nativescript.org/?template=play-ng&id=PmLIFr&v=2 我想在 AuthGuard 中添加一个条件,如果我在 resetPasswordRequest/:id 中有一个 id,我想直接在 ResetPassIdComponent 中导航。在我的演示中,假设 Id 为 123,应直接导航到组件 ResetPassIdComponent
  • 你能看到我的演示吗 github.com/binaau/auth_guard 安装后,请点击这个链接 stackoverflow.com/questions/53921510 并使用应用程序打开。此时,您可以看到该应用程序首先在登录组件中导航,然后在重置通道中进行第二次导航。我只想在单击链接时直接在重置通道中导航。谢谢!

标签: angular routing nativescript canactivate


【解决方案1】:

首先你需要在resetPasswordRequest/:id 中获取参数,如果login 获取参数如下并将字符串转换为数字。您必须将身份验证令牌存储在浏览器缓存中。

    // import in resetPasswordeRquest Component.
    import { Route, AcivatedRoute } from '@angular/router'; 

    // inside component class;
    id: number;
    constructor(private router: Router, private route: ActivatedRoute){
       this.route.paramMap.subscribe(params => {
          this.id = +params.get('id');   
       }           
    }

【讨论】:

  • 我可以在resetPasswordRequest中获取参数。如果我有一个 ID,我只想在 canActivate 中创建一个条件,我的应用程序在 resetPasswordRequest 中导航,否则在 this.router.navigate(['/outsidelogin/login']); Also, I have a aplication mobile in Nativescript 中导航
  • 为什么不在路由器中使用 authguard?比如“{ path: 'resetPasswordRequest/:id', component: ResetPassIdComponent, canActivate: [AuthGuard]};
  • 我使用它但没有任何反应,是一个类似的问题。因为,在 AuthGuard 中我没有这个 id 的控制器的条件
  • 如果我错了,请纠正我,1. 你有登录组件和注册组件的第一页。 2.登录成功后进入resetpasswordRequest组件 3.登录失败后进入注册组件。还有一件事是你应该检查 not authuncated 并返回 false 而不是先返回 true。
  • 1.我有登录组件和注册组件的第一页。 . 2.登录成功后转到(FirstPageComponent),否则转到LoginFirstComponent。 3. 在登录组件中,我有一个重置密码的链接,当单击按钮时,我会在电子邮件中发送一个链接,当我单击电子邮件中的链接时,我想进入 ResetPassIdComponent。问题出在这部分。我单击电子邮件中的链接,首先打开应用程序和应用程序,然后转到 LoginFirstComponent,然后在 ResetPassIdComponent 中打开。我想直接进入ResetPassIdComponent
猜你喜欢
  • 2018-08-08
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 2020-10-28
  • 2019-08-23
  • 2020-11-17
相关资源
最近更新 更多