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

时间:2019-04-19 08:20:55

标签: typescript authentication npm aurelia aurelia-binding

我正在使用授权进行导航。我做了这样的事

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 -我已检查并返回了我的价值

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

1 个答案:

答案 0 :(得分:1)

我想您可以将.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[];
  ...  
}

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