@CanActivate()
통과가 호출되는 구성 요소로 라우팅되는지 여부.
import {appInjector} from './app-injector';
// ...
@CanActivate((next, prev) => {
let injector = appInjector();
let userService: UserService = injector.get(UserService);
// if the checkLogin() method returns a boolean, you can just return it
return userService.checkLogin();
}
당신은 부트 스트랩에서 현재의 인젝터를 유지하기 위해 app-injector.ts
가 필요합니다
let appInjectorRef;
export const appInjector:any = (injector = false) => {
if (!injector) {
return appInjectorRef;
}
appInjectorRef = injector;
return appInjectorRef;
};
당신의 bootstrap
전화에 바로 저장할 인젝터를 :
import {appInjector} from './app-injector';
// ...
bootstrap(App, [
[...],
UserService
]).then((appRef) => appInjector(appRef.injector));
그리고 CanActivate를 통해 가십시오! 404 : –
다운 체크 할 수 있습니다. –