0

我正在尝试构建一个路由保护类,只允许管理员用户访问特定页面。这是我的 admin.guard.ts

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { UserService } from '../services/user.service';

@Injectable()
export class AdminGuard implements CanActivate{
    constructor(
        private router:Router,
        private userService:UserService,
    ){

    }

    canActivate():boolean{
        return this.userService.currentUserIsAdmin();
    }
}

这是 admin.guard.ts 调用的我的用户服务(我只是在这个阶段硬编码一个真或假,直到我让警卫工作)。

import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { MyAppUser } from '../models/MyAppUser';

@Injectable()
export class UserService {

  users:FirebaseListObservable<any[]>;
  user:FirebaseObjectObservable<any>;
  currentUserIsAdmin:boolean;

  constructor(
    public af:AngularFireDatabase
  ) { 
    this.users = this.af.list('/users') as FirebaseListObservable<MyAppUser[]>; 
    this.currentUserIsAdmin = true;
  }

  getUsers(){
    return this.users;
  }

  isCurrentUserAdmin():boolean{
    return this.currentUserIsAdmin;
  }
}

我遇到的问题是以下错误

ERROR in C:/projects/xyz/src/app/guards/admin.guard.ts (16,24): 
Cannot invoke an expression whose type lacks a call signature. 
Type 'Boolean' has no compatible call signatures.
4

1 回答 1

0

您正在调用布尔属性currentUserIsAdmin作为方法。您想调用实际方法而不是isCurrentUserAdmin

canActivate():boolean{
    return this.userService.currentUserIsAdmin();
}

应该成为

canActivate():boolean{
    return this.userService.isCurrentUserAdmin();
}
于 2017-08-06T05:14:41.797 回答