![](/img/trans.png)
[英]How to redirect another component when canActivate returns false?
[英]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.