我有这个AuthGuard。在此代码中,我想创建一个条件。如果我的resetpasss不为null,我想在这里浏览ResetPassIdComponent,否则我想在LoginFirstComponent中浏览。
为此,我创建了AuthGuard。
export class AuthGuard implements CanActivate {
myappurl: any;
resetpasss: any;
constructor(private router: Router, private auth: LoginService) {
}
geturl() {
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL1', appURL);
this.myappurl = appURL
let url_1 = this.myappurl.toString();
let url_id = url_1.split("/").reverse()[0];
this.resetpasss = url_id
let LS = require("nativescript-localstorage");
LS.setItem(this.resetpasss)
// this.router.navigateByUrl('/resetPasswordRequest/' + this.resetpasss);
console.log('this.resetpass1', this.resetpasss)
});
return true;
}
canActivate(): boolean {
this.geturl();
if (this.auth.isAuthenticated()) {
return true;
}
console.log('this.resetpasss2', this.resetpasss)
this.router.navigate(['/outsidelogin/login']);
return false;
}
}
我单击了电子邮件中的链接,该链接在geturl(){..}
中显示了我。通过此功能geturl(){..}
,我得到一个ID并保存在localstorage中。现在,在canActivate()中,我想创建一个条件,以在所需的组件中进行导航。
您能问我如何创建条件的想法吗?
我的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 }
];
答案 0 :(得分:0)
您可以在 CanActivate()本身中添加条件。您可以尝试以下方法吗?
canActivate(): boolean {
this.geturl();
if (this.auth.isAuthenticated()) {
return true;
} else if(this.resetpasss){
this.router.navigate([this.myappurl]);
}else{
this.router.navigate(['/outsidelogin/login']);
}
}