我正在尝试构建路由防护类,仅允许管理员用户访问特定页面。这是我的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.
答案 0 :(得分:0)
您正在调用布尔属性 currentUserIsAdmin 作为方法。您想要调用实际方法而不是 isCurrentUserAdmin
canActivate():boolean{
return this.userService.currentUserIsAdmin();
}
应该成为
canActivate():boolean{
return this.userService.isCurrentUserAdmin();
}