Angular2:将服务注入到防护中时无法解析所有参数

时间:2016-09-14 13:29:13

标签: angular typescript angular2-routing

我正在将我的angular2应用程序更新为RC.7,并且在我允许访问该页面之前检查用户权限时,我遇到了警告。现在我已经简化了后卫,只是总是返回true而且它仍然无法工作 - 看起来它找不到我的appStateService,但是我已将它注入到我的应用程序中的其他组件和服务中,并且在那里工作正常。

守卫代码:

import { Inject, Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

import { AppStateService } from '../../appState';

@Injectable()
export class MonitorPermissionGuard implements CanActivate {

constructor(private appStateService: AppStateService, private router: Router) {}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    console.log('guard called');
    return true;
}

}

我的app.module.ts文件已将AppStateService和MonitorPermissionGuard添加到providers数组中。

运行应用时,我的控制台出现以下错误:

metadata_resolver.js:523 Uncaught Error: Can't resolve all parameters for MonitorPermissionGuard: (?, Router).

不知道这里发生了什么,因为我在整个应用程序中使用了appStateService而没有任何问题。

由于

编辑:AppStateService的代码

@Injectable()
export class AppStateService {

public appState: AppStateModel;

constructor() {
    this.appState = new AppStateModel();
}

toggleSidenav() {
    this.appState.sidenavExpand = !this.appState.sidenavExpand;
}

clearSidenav() {
    this.appState.sidenavButtons = new Array<SidenavButton>();
}

addSidenavLink(buttonText: string, iconClass: string, route: string) {
    let sidenavButton: SidenavButton = new SidenavButton(buttonText, iconClass, route);
    this.appState.sidenavButtons.push(sidenavButton);
}

setPageTitle(pageTitle: string) {
    this.appState.pageTitle = pageTitle;
}

hasPermission(feature: string) {
    return this.appState.userPermissions[feature];
}

giveFeaturePermission(feature: string) {
    this.appState.userPermissions[feature] = true;
}

recantFeaturePermission(feature: string) {
    this.appState.userPermissions[feature] = false;
}

showInfo(message: string) {
    this.showMessage('INFO', message);
}

showWarning(message: string) {
    this.showMessage('WARNING', message);
}

showError(message: string) {
    this.showMessage('ERROR', message);
}

private showMessage(type: string, message: string) {
    this.appState.appMessage.type = type;
    this.appState.appMessage.message = message;
    this.appState.isMessageShown = true;
    setTimeout(() => { this.appState.isMessageShown = false; }, 2000);
}

}

0 个答案:

没有答案