我正在开发angular2 app。我按照@brandonroberts的例子来限制用户访问他不被允许的页面。
http://plnkr.co/edit/SF8gsYN1SvmUbkosHjqQ?p=preview
我的问题是当我需要使用RouteParams从url获取参数时,注入器不会解析RouteParams。
在isLoggedIn常量中,我需要使用RouteParams从url获取参数 当我尝试let routeParams:RouteParams = injector.get(routeParams);它会抛出异常。
import {Injector} from 'angular2/core';
import {appInjector} from './app-injector';
import {Auth} from './Auth';
import {Router, ComponentInstruction,RouteParams} from 'angular2/router';
export const isLoggedIn = (next: ComponentInstruction, previous: ComponentInstruction) => {
let injector: Injector = appInjector(); // get the stored reference to the injector
let auth: Auth = injector.get(Auth);
let router: Router = injector.get(Router);
// return a boolean or a promise that resolves a boolean
return new Promise((resolve) => {
auth.check()
.subscribe((result) => {
if (result) {
resolve(true);
} else {
router.navigate(['/Login']);
resolve(false);
}
});
});
};