JSON 中 angular 中 JSON.parse (<anonymous>) 位置 0 的意外标记 F
Unexpected token F in JSON at position 0 at JSON.parse (<anonymous>) in angular
我正在尝试在我的 project.I 中实现基于角色的授权,我正在尝试根据角色从菜单项的导航栏中隐藏某些项目。
我遇到了上述错误。我该如何解决?
service.ts
roleMatch(allowedRoles):boolean{
var isMatch =false;
var userRoles:string[]=JSON.parse(localStorage.getItem('userroles')); //the error is here
allowedRoles.forEach(element => {
if(userRoles.indexOf(element)>-1){
isMatch=true;
return false;
}
})
return isMatch;
}
component.html
<li *ngIf="Authentication.roleMatch(['Fetch user',
'add user',
'edit user',
'change status',
'delete user',
'delete role',
'Fetch Recuirtmentdetails'])">
<a routerLink="/role"class="dropdown-item">User Role</a>
</li>
正如你所说userroles的值是获取用户,添加用户,编辑用户,更改状态,删除用户,删除角色,获取招聘详情
它是 String,您正在对它执行 JSON.parse()。
您只需要 var userRoles:string[] 即可执行以下操作:
var userRoles:string[] = localStorage.getItem('userroles').split(',');
你会得到你想要的结果。
我正在尝试在我的 project.I 中实现基于角色的授权,我正在尝试根据角色从菜单项的导航栏中隐藏某些项目。 我遇到了上述错误。我该如何解决?
service.ts
roleMatch(allowedRoles):boolean{
var isMatch =false;
var userRoles:string[]=JSON.parse(localStorage.getItem('userroles')); //the error is here
allowedRoles.forEach(element => {
if(userRoles.indexOf(element)>-1){
isMatch=true;
return false;
}
})
return isMatch;
}
component.html
<li *ngIf="Authentication.roleMatch(['Fetch user',
'add user',
'edit user',
'change status',
'delete user',
'delete role',
'Fetch Recuirtmentdetails'])">
<a routerLink="/role"class="dropdown-item">User Role</a>
</li>
正如你所说userroles的值是获取用户,添加用户,编辑用户,更改状态,删除用户,删除角色,获取招聘详情
它是 String,您正在对它执行 JSON.parse()。
您只需要 var userRoles:string[] 即可执行以下操作:
var userRoles:string[] = localStorage.getItem('userroles').split(',');
你会得到你想要的结果。