开始尝试使用Angular2。在调用this.auth.isLoggedIn()
时,错误发生在此块中。我收到Property 'isLoggedIn' does not exist on type 'AuthService'.
错误。为什么函数调用被解释为属性访问?
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate() {
if(!this.auth.isLoggedIn()) {
this.router.navigate[''];
return false;
}
}
}
这是注入的服务:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
@Injectable()
export class AuthService {
private loggedIn:boolean = false;
constructor(private router: Router) {
this.loggedIn = !!localStorage.getItem('auth_token');
}
login(email:string, password:string):void {
localStorage.setItem('auth_token', 'x');
this.loggedIn = true;
}
logout():void {
localStorage.removeItem('auth_token');
this.loggedIn = false;
}
isLoggedin():boolean {
return this.loggedIn;
}
}
答案 0 :(得分:3)
请将您服务的方法签名从isLoggedin() : boolean
更改为isLoggedIn() : boolean
。