Как выполнить маршрутизацию в компоненте при использовании AuthGuard в Nativescript
Я использую nativescript-urlhandler в своем приложении Nativescript. Когда я ставлю роутер, мое приложение направляет сначала в LoginFirstComponent, а во вторую в ResetPassIdComponent, который я хочу.
Я хочу направить непосредственно к компоненту, который я хочу.
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{
path: 'fp', component: FirstPageComponent
},
{
path: 'profile', component: ProfileComponent
}
]
},
{
path: 'outsidelogin',
component: outsideloginComponent,
canActivate: [AuthGuardReset],
children: [
{ path: 'login', component: LoginFirstComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
]
},
{ path: '', redirectTo: '/home/fp', pathMatch: 'full' }
];
AuthGuard.ts
canActivate(): boolean {
if (this.auth.isAuthenticated()) {
return true;
}
this.router.navigate(['/outsidelogin/login']);
return false;
}
AuthGuardReset.ts
canActivate(): boolean {
if (this.auth.isAuthenticated()) {
return true;
}
this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
return false;
}
Мой AuthGuardReset.ts не переходит на ResetPassIdComponent, он остается в LoginFirstComponent.
В component.ts у меня есть этот код:
ngOnInit() {
if (this.auth.isAuthenticated()) {
return true;
}
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
this.myappurl = appURL
let url_1 = this.myappurl.toString();
let url_id = url_1.split("/").reverse()[0];
this.resetpasss = url_id
this.router.navigateByUrl('/outsidelogin/resetPasswordRequest/' + this.resetpasss);
});
}
Можете ли вы поделиться со мной идеей, как решить эту проблему, пожалуйста?
1 ответ
Попробуйте это:- возвращение истинного вместо ложного
canActivate(): boolean {
if (this.auth.isAuthenticated()) {
return true;
}
this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
return true;
}
Пожалуйста, обратитесь к этой ссылке для получения более подробной информации. Как использовать угловые 6 Route Auth Guard для всех маршрутов Root и Child Routes?