在这部分中,我可以展示角色的许可。喜欢这个
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(Api.getUrl(Api.URLS.Login), body, {
headers: headers
})
.map((response: Response) => {
let res = response.json();
let permissions: any[] = res.StatusDescription.Permisions;
console.log(permissions)
if (res.StatusCode === 0 && res.Token) {
this.currentUser = {
username: username,
token: res.Token
}
localStorage.setItem(AuthService.CURRENT_USER, JSON.stringify(this.currentUser));
return true;
} else {
return false;
}
}
);
现在我想创建在这个数组上找到元素的函数!你能建议我,如何实施?
答案 0 :(得分:1)
<强> auths.component.ts 强>
export class AuthsComponent {
permissions:string[] = [];
findPermission(permission:string){
return this.permissions.find(p => p===permission)
}
// this is your function with some modification
call(){
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(Api.getUrl(Api.URLS.Login), body, {
headers: headers
})
.map((response: Response) => {
let res = response.json();
// HERE : set the permissions value
this.permissions = res.StatusDescription.Permisions;
console.log(permissions)
if (res.StatusCode === 0 && res.Token) {
this.currentUser = {
username: username,
token: res.Token
}
localStorage.setItem(AuthService.CURRENT_USER, JSON.stringify(this.currentUser));
return true;
} else {
return false;
}
}
);
}
}
<强> auths.component.html 强>
<span *ngIf="findPermission('app_usersgetall')"></span>