Angular 如何在第一个函数和第二个canActivate()中执行
我有这个警卫。在这段代码中,我想创建一个条件。如果我的resetpasss不为null,我想在这里导航ResetPassIdComponent,否则我想在LoginFirstComponent中导航 为此,我创建了AuthGuardAngular 如何在第一个函数和第二个canActivate()中执行,angular,typescript,nativescript,canactivate,Angular,Typescript,Nativescript,Canactivate,我有这个警卫。在这段代码中,我想创建一个条件。如果我的resetpasss不为null,我想在这里导航ResetPassIdComponent,否则我想在LoginFirstComponent中导航 为此,我创建了AuthGuard export class AuthGuard implements CanActivate { myappurl: any; resetpasss: any; constructor(private router: Router, priva
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()中,我想创建一个条件,在我想要的组件中导航
你能问我如何创造条件吗
我的旅行路线
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()自身中添加条件。你能像下面那样试一下吗
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']);
}
}
是的,但是当我单击链接时如何在此组件ResetPassIdComponent中直接导航?
此.ResetPass
未定义,并且在此.router.navigate(['/outsidelogin/login'])中直接导航代码>你能看到我的演示吗请github.com/binaau/auth_guard安装后,请单击此链接stackoverflow.com/questions/53921510并使用应用程序打开。此时,您可以看到应用程序首先在登录组件中导航,然后在重置过程中导航。我只想在单击链接时直接在重置过程中导航。非常感谢。handleOpenURL
是异步的吗?我使用这个插件你在控制台中有错误吗?this.resetpasss2是未定义的@ArtyomAmiryan问你的handleOpenURL()是异步的吗?