I'm updating my angular2 application to RC.7 and I've hit a snag with my guard that checks a user permission before allowing access to the page. Right now I've simplified the guard to just return true always and it's still not working - looks like it can't find my appStateService, but I have injected it into other components and services in my app and it works ok there.
Guard Code:
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;
}
}
My app.module.ts file has both the AppStateService and the MonitorPermissionGuard added to the providers array.
I get the following error in my console when I run the app:
metadata_resolver.js:523 Uncaught Error: Can't resolve all parameters for MonitorPermissionGuard: (?, Router).
Not sure whats going on here because I've used the appStateService throughout the app with no problems.
Thanks
EDIT: Code for 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);
}
}