【发布时间】:2019-01-07 07:16:34
【问题描述】:
我正在使用 Angular 开发一个 Web 应用程序,但遇到了一个问题。
有一个登录用户的身份验证服务。我正在向
具有凭据并等待响应的服务器。问题是我
尝试从登录组件导航到主组件在订阅中
回复的
login(formValues) {
this.auth.logInUser(formValues.username, formValues.password)
.subscribe(response =>{
if(!response){
this.invalidLogin=true;
} else {
this.router.navigate(['stream']);
}
})
}
但是每个其他组件都有一个 canActivateGuard 检查当前用户是否登录(我正在从服务器等待的数据)。
export const appRoutes: Routes = [
{path: 'login', resolve: LiveStreamResolver, component: LoginComponent},
{path: 'reports', component: ReportsComponent, resolve: LiveStreamResolver, canActivate: [AuthGuardService]},
{path: 'calendar', component: CalendarComponent, resolve: LiveStreamResolver, canActivate: [AuthGuardService]},
{path: 'stream', component: LiveStreamComponent},
{path: '', redirectTo: 'login', pathMatch: 'full'}
];
constructor(public auth: AuthenticationService) { }
canActivate(): boolean {
return !!this.auth.currUser;
}
有没有办法在 canActivate 检查完成之前解决?还有其他解决方案吗?
欢迎任何其他有关如何保护组件的建议:D
【问题讨论】:
-
请检查更新后的答案:)
标签: angular authentication observable resolve canactivate