Aurelia - 在导航菜单中隐藏路线最终什么都不显示

Aurelia - Hiding routes in navmenu ends up displaying nothing

我想隐藏作为用户角色的路由,我在 SO 上发现了这个类似的问题。我试图在我的打字稿项目中实现它,但它什么也没返回,我不确定为什么。

这是我的实现。

import { autoinject, bindable, bindingMode } from "aurelia-framework";
import { Router } from 'aurelia-router'

@autoinject
export class Navmenu {
 public userName: string = 'anonymous';
 private userRole = localStorage.getItem("user_role");


 constructor(public authService: AuthService, public router: Router) {
     this.userName = authService.getUserName();
     console.log("userRole: ", this.userRole);
 }

 get routes() {
     return this.router.navigation.filter(r => r.settings.roles === this.userRole );
 }
}

我的 console.log 在控制台中显示 "Admin" 但我的过滤器没有过滤它。

这是我构建路线的方式:

        {
            route: ["", "scheduler"],
            name: "scheduler",
            settings: {
                icon: "scheduler",
                auth: true,
                roles: ["Employee", "Admin"],   //These are my roles for this route.
                pos: "left"
            },
            moduleId: PLATFORM.moduleName("../components/scheduler/scheduler"),
            nav: true,
            title: "scheduler"
        },

角色是一个数组。

我如何构造我的过滤器以匹配任何用户角色,从而匹配 returns 过滤路由的子集?

查看路由器配置中的这一行:

roles: ["Employee", "Admin"]

然后在你的 getter:

r.settings.roles === this.userRole

roles 是一个数组,而 this.userRole 是一个字符串,因此 === 运算符将始终 return 为 false。使用 indexOfsome 代替:

return this.router.navigation.filter(r => r.settings.roles.indexOf(this.userRole) > -1);

return this.router.navigation.filter(r => r.settings.roles.some(t => t === this.userRole));