Angular2使用路由器数据中的函数?

时间:2017-03-06 13:34:49

标签: angular angular-ui-router

我认为这个伪代码应该清楚问题。无法找到解决方案。怎么解决这个问题?

目前的工作路线:

{
    path: ':xyzType',
    component: SuperSpecialComponent,
    canActivate: [AuthGuard],
    data: {

      /* ENUM 1 for XOR */
      authPermissionType: authPermissionType["XOR"],

      permissions: [
        'add:xyzType1',
        'edit:xyzType1',

        'add:xyzType2',
        'edit:xyzType2'
      ]
    },
}

我希望权限可以依赖于路由参数' xzyType'来设置。应该是这样的:

{
    path: ':xyzType',
    component: SuperSpecialComponent,
    canActivate: [AuthGuard],
    data: {

      /* ENUM 1 for XOR */
      authPermissionType: authPermissionType["XOR"],

      permissions: function(this_route.param['xyzType']) {
          if(this_route.param['xyzType'] == 'type1' {
              return [
                  'add:xyzType1',
                  'edit:xyzType1'
              ]
          } 
          else if(this_route.param['xyzType'] == 'type2' {
              return [
                  'add:xyzType2',
                  'edit:xyzType2'
              ]
          } 
      }
    },
}

1 个答案:

答案 0 :(得分:0)

所以我尝试为此设置一个解决方案,结果是另一个问题:

{
    path: ':xyzType',
    component: SuperSpecialComponent,
    canActivate: [AuthGuard],
    data: {

      /* ENUM 1 for XOR */
      authPermissionType: authPermissionType["XOR"],

      permissions: {
        "type1": [
          "add:xyzType1",
          "edit:xyzType1"
        ],
        "type2": [
          "add:xyzType2",
          "edit:xyzType2"
        ]
      }
    },
    resolve: {
      permissions: PermissionsResolver
    }
}

在解析器内部,我检查路径参数并使用以下代码返回所需的权限:

switch(route.params[xyzType]) {
  case 'type1':
    return route.data['permissions']['type1'];

  case 'type2':
    return route.data['permissions']['type2'];
}

问题是即使解决方案尚未完成,AuthGuard也会检查权限。有没有办法告诉AuthGuard等待权限改变?