Angular 2 router send data with resolver don't work

376 views Asked by At

My codes like that but don't working. I want to check user info before route loading.

I use @angular/router:3.4.2

Resolver Class:

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

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

Router:

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

Secure Component:

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');
    }
  }
}
1

There are 1 answers

0
vangoo On

İ solve my problem. It's in secure component:

I add private route: ActivatedRoute in constructor

And call in ngOnInıt()

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