函数在Angular2中被误解为属性?

时间:2017-01-26 04:21:29

标签: angular

开始尝试使用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;
  }
}

1 个答案:

答案 0 :(得分:3)

请将您服务的方法签名从isLoggedin() : boolean更改为isLoggedIn() : boolean