Angular Rxjs find运算符不处理可观察数组中的值
我有一个服务,在该服务中,我将我的http有效负载投射到一个可观察数组,我希望能够调用该服务中的一个方法来对该可观察数组进行操作。在我的例子中,我想确认一个ID存在于可观察ID的数组中。我对stackoverflow做了一些研究,我认为这是因为我错误地处理了find操作符的返回(据我所知,它返回一个可观察的) 服务:Angular Rxjs find运算符不处理可观察数组中的值,angular,rxjs,Angular,Rxjs,我有一个服务,在该服务中,我将我的http有效负载投射到一个可观察数组,我希望能够调用该服务中的一个方法来对该可观察数组进行操作。在我的例子中,我想确认一个ID存在于可观察ID的数组中。我对stackoverflow做了一些研究,我认为这是因为我错误地处理了find操作符的返回(据我所知,它返回一个可观察的) 服务: @Injectable({ providedIn: 'root' }) export class RuleFieldsService { baseUrl: string;
@Injectable({
providedIn: 'root'
})
export class RuleFieldsService {
baseUrl: string;
relativeUrl: string;
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
private rulefieldIdSub = new BehaviorSubject<any[]>([]);
private rulefieldIdStore: { ruleFieldIds: any } = { ruleFieldIds: [] };
rulefieldIds$ = this.rulefieldIdSub.asObservable();
constructor(private readonly http: HttpClient, @Inject(APP_BASE_HREF)
private readonly baseHref: string)
{
this.baseUrl = this.baseHref + environment.baseAPIUrl;
};
getRuleFields() {
this.relativeUrl = 'rulefields';
return this.http.get(this.baseUrl + this.relativeUrl);
}
getList(){
this.relativeUrl = 'rulefields';
this.http.get(this.baseUrl + this.relativeUrl + '/enabledropdown').subscribe(data => {
this.rulefieldIdStore.ruleFieldIds = data;
this.rulefieldIdSub.next(Object.assign({}, this.rulefieldIdStore).ruleFieldIds);
});
}
checkRuleFieldType(ruleFieldId) {
console.log('check method')
this.rulefieldIds$.pipe(find((id: any) => id === ruleFieldId)).subscribe(items => {
console.log("ID is in the list")
});
}
}
提前感谢您的洞察力 您理解了“查找”操作符有点不正确。它是一个过滤器,用于过滤发出多个值的数据流,但在值与谓词匹配并停止发出之前不发出任何内容 您的示例observable发出一个值,恰好是一个数组。在函数中要做的是使用rxjs映射运算符,在其中包含值数组,并使用标准数组的find(或findIndex)返回布尔值 我已经准备了一个小的stack blitz演示它,并附上一些评论 以下是我提到的一些概念和运算符:
导出类示例服务{
//从“null”开始,这样我们就可以过滤掉它,这样我们就知道get列表至少运行过一次,如果这很重要的话
private rulefieldIdSub=新行为主体(null);
//我们跳过初始的“null”值,这确保“getList”已经填充了规则
rulefieldIds$=this.rulefieldIdSub.asObservable().pipe(过滤器(ids=>ids!==null));
getList(){
//例如,制作一个假列表,在设置的超时时间内运行,因此需要一秒钟
设置超时(()=>{
this.rulefieldsub.next([1,3,5,7,9])
}, 1000);
}
checkRuleFieldType(ruleFieldId):可观察{
返回此.rulefieldIds$.pipe(
//我们正在使用“映射”操作符将结果从可观察的转化为其他的东西
// https://rxjs-dev.firebaseapp.com/api/operators/map
映射(RuleFieldId=>{
//map获取规则域id的完整数组,现在我们返回是否找到该id
返回ruleFieldId.findIndex(id=>id==ruleFieldId)!=-1;
})
)
}
}
以及使用它的更新组件
导出类AppComponent{
名称='角度';
rulefieldIds$:可观察;
构造函数(公共exampleService:exampleService){
这个.exampleService.getList();
this.rulefieldIds$=this.exampleService.rulefieldIds$;
this.exampleService.checkRuleFieldType(15).subscribe(结果=>{
log(“我们有15个吗?”,结果)
});
this.exampleService.checkRuleFieldType(5).subscribe(结果=>{
log(“我们有5个吗?”,结果)
});
}
}
谢谢,这无疑是朝着正确方向迈出的一步,比以前更有意义!然而,我得到的结果是:ruleFieldIds.findIndex不是一个函数,同样,如果我尝试使用.find。我是否仍然需要使用:private rulefieldIdStore:{ruleFieldIds:any}={ruleFieldIds:[]};,来实现我的observable?如果您使用非数组的内容初始化BehaviorSubject,并且没有将其过滤掉或在映射中检查它,则可能会出现类似的错误。在我的示例中,当我创建rulefieldIds$时,我会进行筛选以确保其不为null,这是怎么回事!清晰,哈扎赫!谢谢你,先生,我在20分钟内从你那里学到了比4个小时的艰苦研究和故障排除更多的东西。现在我可以确保我的团队坚持这种做法了,哈哈:)。很高兴我能帮上忙!当您需要使用其中一项功能时,请始终查看或查看。他们倾向于用图表解释他们的工作原理和例子。如果有疑问,请记录运算符中的值,它可能不是您期望的类型。这些图表当然有帮助,我几乎被它们的用处吓了一跳:)。
@Component({
selector: 'app-rules-tab-wrapper',
templateUrl: './rules-tab-wrapper.component.html',
styleUrls: ['./rules-tab-wrapper.component.scss']
})
export class RulesTabWrapperComponent implements OnInit {
public selectedIndex;
public ruleFieldIds$: Observable<any[]>;
constructor(private rulesetService: RulesetService,
private rulefieldService: RuleFieldsService) { }
ngOnInit() {
this.rulefieldService.getList();
this.ruleFieldIds$ = this.rulefieldService.rulefieldIds$;
this.rulefieldService.checkRuleFieldType(15);
console.log(this.ruleFieldIds$ )
}
}
source: BehaviorSubject
closed: false
hasError: false
isStopped: false
observers: Array(1)
0: FindValueSubscriber
closed: false
destination: Subscriber {closed: false, _parentOrParents: null, _subscriptions: Array(1), syncErrorValue: null, syncErrorThrown: false, …}
index: 2
isStopped: false
predicate: (id) => id === ruleFieldId
source: Observable
source: BehaviorSubject {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
_isScalar: false
__proto__: Object
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
thisArg: undefined
yieldIndex: false
_parentOrParents: Subscriber
closed: false
destination: SafeSubscriber {closed: false, _parentOrParents: null, _subscriptions: null, syncErrorValue: null, syncErrorThrown: false, …}
isStopped: false
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
_parentOrParents: null
_subscriptions: [FindValueSubscriber]
__proto__: Subscription
_subscriptions: [SubjectSubscription]
__proto__: Subscriber
length: 1
__proto__: Array(0)
thrownError: null
_isScalar: false
_value:
enableDropdownFor: (6) [15, 42, 55, 65, 67, 69]