Aurelia TS 从地图中拉取地图

Aurelia TS pulling map from a map

我在我的 appConfig.json 中定义了一个 Map<string, Map<string, boolean>>,当我尝试使用 .get() 从地图中获取值时,我得到一个错误,它不是一个函数。

这是我的 appConfig.json

"rolePermissions" : [
    {"name" : "Admin", "value" : [
      {"name" : "customerList", "value" : "true"},
      {"name" : "customerDetails", "value" : "true"},
      {"name" : "addCustomerUser", "value" : "true"},
      {"name" : "editCustomer", "value" : "true"},
      {"name" : "updateBackend", "value" : "true"},
      {"name" : "createCustomer", "value" : "true"},
      {"name" : "jobsList", "value" : "true"},
      {"name" : "jobsView", "value" : "true"},
      {"name" : "jobsEdit", "value" : "true"},
      {"name" : "jobsDownload", "value" : "true"},
      {"name" : "usersList", "value" : "true"},
      {"name" : "usersEdit", "value" : "true"},
      {"name" : "usersDelete", "value" : "true"},
      {"name" : "usersCreate", "value" : "true"}
    ]}

并且我已将 appConfig.ts 中的 属性 设置为: public角色权限:地图>;

这就是我访问 rolePermissions 的方式:

let rolePerms = this.appConfig.rolePermissions;

let rolePerm = rolePerms.get("Admin");

当我尝试从地图中提取 "Admin" 值时,出现错误:

ERROR [app-router] TypeError: rolePerms.get is not a function
    at Utils.userRoleAuthorized (utils.ts:29)
    at NewCustomer.canActivate (newCustomer.ts:42)
    at iterate (index.ts:7)
    at processActivatable (index.ts:7)
    at CanActivateNextStep.run (index.ts:7)
    at next (index.ts:7)
    at Object.<anonymous> (shell.ts:58)
    at step (vendor-bundle.js:4344)
    at Object.next (vendor-bundle.js:4325)
    at vendor-bundle.js:4318
    at Promise._execute (vendor-bundle.js:824)
    at Promise._resolveFromExecutor (vendor-bundle.js:2601)
    at new Promise (vendor-bundle.js:2197)
    at __awaiter (vendor-bundle.js:4314)
    at Object.run (shell.ts:45)
    at next (index.ts:7)

编辑:

我没看出来你的文件是 json,但原来的 post 仍然适用。

如果您无法将 rolePermissions 转换为 Map,则需要像处理常规数组一样处理它:

rolePerms.find(x => x.name === "Admin").value

原文:

你的初始化方式 rolePermissions 是你的问题。 您实际上不是在创建一个 Map,而是一个数组。 因此函数 get 不可用,因为 get 不是在数组原型上定义的函数。

以下是您需要执行的操作:

const rolePermissions = new Map<string, Map<string, boolean>>(
    [
        [
            "Admin", new Map<string, boolean>([
                ["customerList", true],
                ["customerDetails", true],
                ["addCustomerUser", true],
                ["editCustomer", true],
                ["updateBackend", true],
                ["createCustomer", true],
                ["jobsList", true],
                ["jobsView", true],
                ["jobsEdit", true],
                ["jobsDownload", true],
                ["usersList", true],
                ["usersEdit", true],
                ["usersDelete", true],
                ["usersCreate", true]
            ])
        ]
    ]);