私はtypescriptで角度2でアプリケーションを作成し、認証を追加します。ang2でcanActivated
私は私のルーティングファイルのauthguardを作成します、
{ canActivate: [AuthGuard], component: LaskComponent, path: "table_per" }
そして今:私は私のルートファイルにauthguardを追加
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;
}
});
}
:
@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()
機能ですこのページをロードするときlocalhost/table_per
私はこのエラーがあります: TypeError: Cannot read property 'isLogin' of undefined
。 なぜそれが起こるのか本当に分かります。あなたがこのことから、あなたの条件を簡素化することができ、また
@Injectable()
export class AuthGuard implements CanActivate {
public router: Router;
public serverThisLogin: ServerDataComponent;
constructor(private router: Router, private serviveThisLogin: ServerDataComponent) {}
public canActivate(): boolean {
if (this.serverThisLogin.isLogin) {
return true;
}
this.router.navigate(["/login"]);
return false;
}
}
:
... – Dinistro