2017-02-07 9 views
0

내 코드는 작동하지만 작동하지 않습니다. 경로로드 전에 사용자 정보를 확인하고 싶습니다. 3.4.2해결사로 각도 2 라우터 전송 데이터가 작동하지 않습니다.

확인자 클래스 :

@Injectable() 
export class UserRoleResolver implements Resolve<any> { 

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any { 
    let user:any = {}; 
    user.role = 'MEMBER'; 
    return user; 
    } 
} 

라우터 :

export const APP_ROUTES: Routes = [ 
    {path: '', component: PublicComponent, children: PUBLIC_ROUTES}, 
    { 
    path: '', 
    component: SecureComponent, 
    resolve: {userRole: UserRoleResolver}, 
    canActivate: [AuthGuard], 
    children: SECURE_ROUTES 
    } 
]; 

안전 구성 요소 : 나는 각도/라우터 @ 사용

export class SecureComponent implements OnInit { 
    userRole:any; 
    role:string = 'MEMBER'; 
    constructor(private userService: UserService,private router:Router) { 
    } 

    ngOnInit() { 

    this.role = 'MEMBER'; 
    console.log('Role',this.userRole); 

    if (!this.userService.isLoggedIn()) { 
     this.router.navigateByUrl('/login'); 
    } 
    } 
} 

답변

0

내가 해결 내 문제. 그것은 보안 구성 요소입니다 : 내가 생성자

private route: ActivatedRoute를 추가하고 전화

에서 ngOnInıt()

this.userRole = this.route.snapshot.data['userRole'];