问题
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.
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;
}
}
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?
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 }
];
回答1:
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']);
}
}
来源:https://stackoverflow.com/questions/53921510/how-to-execute-in-first-a-function-and-in-second-canactivate