0
我使用typescript创建了角2中的一些应用程序并添加了身份验证。canActivated in angular2
我为我的路由文件创建authguard:
@Injectable()
export class AuthGuard implements CanActivate {
public router: Router;
public serverThisLogin: ServerDataComponent;
constructor(router: Router) {
this.router = router;
}
public canActivate(): boolean {
if (this.serverThisLogin.isLogin) {
return true;
}
this.router.navigate(["/login"]);
return false;
}
}
这是isLogin()
功能:
public isLogin (username: string, password: string): Observable<boolean> {
return this.http.post(authURL + loginURL,
JSON.stringify({ password, username }))
.map((response: Response) => {
if (response.status === 200) {
return true;
} else {
return false;
}
});
}
我添加authguard在我的路由文件:
{ canActivate: [AuthGuard], component: LaskComponent, path: "table_per" }
而现在,当我加载此页localhost/table_per
我有这个错误: TypeError: Cannot read property 'isLogin' of undefined
。 我真的明白为什么会发生。
你不注入ServerDataComponent ... – Dinistro