繁体   English   中英

使用canActivate()时如何在另一个组件中进行路由

[英]How to routing in another component when use canActivate()

当我使用canActivate()时,我想直接在这里导航组件ResetPassIdComponent 现在,当我单击该组件ResetPassIdComponent时,我的应用程序首先在/outsidelogin/login导航,然后在resetPasswordRequest/:id中第二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']);this.router.navigate(['/outsidelogin/login']);导航this.router.navigate(['/outsidelogin/login']);

我的routing.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 }
];

请,您能和我分享任何想法,如何解决我的问题吗?

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

    // 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');   
       }           
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM