检查设置存储库中的用户角色(打字稿,aurelia)

Checking user role in settings repository (typescript, aurelia)

我正在授权导航。我做了这样的事情

run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {

    let requiredRoles = navigationInstruction.getAllInstructions()
                        .map(i => i.config.settings.roles)[0];

    toastr.success('TEST1');

    let isUserInRole = requiredRoles?   
        requiredRoles.some(r => r === this.userIdentity.role) : true;

    toastr.success('TEST2');//LINE IT`S NOT EXECUTED

    return isUserInRole? next() : next.cancel(new Redirect('users/login'));    
}

requiredRoles - 我已经检查过了,它是 return 我的价值

进入行r => r === this.userIdentity.role 在我的视觉代码中,我有这样的消息:Parameter 'r' implicitly has an 'any' type, but a better type may be inferred from usage.ts(7044)

我想你可以做的是将 .roles)[0] 转换为字符串(我猜从你的代码来看)

run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {

    let requiredRoles = navigationInstruction.getAllInstructions()
                        .map(i => i.config.settings.roles)[0] as string;
  ...  
}

编辑:如果角色是一个字符串数组,那么你的代码应该是这样的

run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {

    let requiredRoles = navigationInstruction.getAllInstructions()
                        .map(i => i.config.settings.roles)[0] as string[];
  ...  
}

这样做应该可以解决问题。