Angular 如何从可观察流中检索特定字段?
我已经创建了以下可观察的流,其中包含用户对象Angular 如何从可观察流中检索特定字段?,angular,typescript,rxjs,observable,angular2-observables,Angular,Typescript,Rxjs,Observable,Angular2 Observables,我已经创建了以下可观察的流,其中包含用户对象 currentUserProfile$ = new BehaviorSubject<UserProfile>(null); 现在我想为流包含的每个属性创建方法。例如 getUserRole(){ let roles:Array<any>; this.currentUserProfile$.subscribe((profile:UserProfile)=>{ roles=profile.rol
currentUserProfile$ = new BehaviorSubject<UserProfile>(null);
现在我想为流包含的每个属性创建方法。例如
getUserRole(){
let roles:Array<any>;
this.currentUserProfile$.subscribe((profile:UserProfile)=>{
roles=profile.roles;
});
return roles;
}
hasRole(role:string):boolean{
let res:boolean;
this.currentUserProfile$.subscribe(
(profile)=>{
res=profile.roles.indexOf(role) !== -1;
}
);
return res;
}
getUserRole(){
让角色:数组;
此.currentUserProfile$.subscribe((配置文件:UserProfile)=>{
角色=profile.roles;
});
返回角色;
}
hasRole(角色:字符串):布尔值{
让res:布尔;
此.currentUserProfile$.subscribe(
(个人资料)=>{
res=profile.roles.indexOf(role)!=-1;
}
);
返回res;
}
还有比这更简单的方法吗
提前感谢我宁愿保持可观察性,他们会对其进行一些转换
getUserRole(){
return this.currentUserProfile.map((profile:UserProfile)=> profile.roles);
}
hasRole(role:string):boolean{
return this.currentUserProfile$.reduce((role) =>
profile.roles.indexOf(role) !== -1;
);
}
getUserRole(){
return this.currentUserProfile.map((profile:UserProfile)=> profile.roles);
}
hasRole(role:string):boolean{
return this.currentUserProfile$.reduce((role) =>
profile.roles.indexOf(role) !== -1;
);
}