问题描述
我有这个AuthGuard.在此代码中,我想创建一个条件.如果我的resetpasss不为null,我想在这里浏览ResetPassIdComponent,否则我想在LoginFirstComponent中浏览.
I have this AuthGuard. In this code I want to create a condition. If my resetpasss is not null I want to navigate in here ResetPassIdComponent else I want to navigate in LoginFirstComponent.
为此,我创建了AuthGuard.
For this I create 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()中,我想创建一个条件,以在所需的组件中进行导航.
I click a link from email, this link show me in geturl(){..}
. From this function geturl(){..}
I get an id and save in localstorage. Now in canActivate() I want to create a condition that navigate me in a component that I want.
您能问我如何创建条件的想法吗?
Can you ask me any idea how to create a condition?
我的routing.ts
My 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 }
];
推荐答案
您可以在 CanActivate()本身中添加条件.你可以像下面这样尝试吗?
You can add the condition in CanActivate() itself. Can you tried like the below?
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']);
}
}
这篇关于如何首先执行一个函数,然后执行第二个canActivate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!