Angular 2路由器使用解析器发送数据不起作用

时间:2017-02-07 16:50:25

标签: angular resolver angular2-router3

我的代码就是这样,但是没有工作。我想在路由加载之前检查用户信息。

我使用@ angular / router:3.4.2

解析器类:

window.myLine = new Chart(chartEl, {
    type: 'line',
    data: lineChartData,
    options: {
        title:{
            display:true,
            text:'Chart.js Line Chart - Custom Tooltips'
        },
        tooltips: {
            enabled: false,
            mode: 'index',
            position: 'nearest',
            //Set the name of the custom function here
            custom: customTooltips
        }
    }
});

路由器:

@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
  }
];

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。它位于安全组件中:

我在构造函数

中添加private route: ActivatedRoute

并致电ngOnInıt()

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