我正在尝试构建一个路由保护类,只允许管理员用户访问特定页面。这是我的 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.