behaivorSubject返回null
我有一个问题,我需要behaivorSubject在null时不返回该值,因为结果被第三方制造的组件抓住,如果这是无效的,它会给我带来错误。
它应该仅在从服务器中获得响应时返回相同的返回,我将代码留在此处:
getCarList(params): Observable<CarListGrid> {
if (this.carListSubject$ === undefined) {
this.carListSubject$ = new BehaviorSubject<CarListGrid>(null);
this.refetchCarList(params);
}
return this.carListSubject$.asObservable();
}
refetchCarList(gridParams) {
const subscription = this.http.post<CarListGrid>(this.baseUrl + 'GetCar', {gridParams} ,httpOptions).pipe(map(data => this.setDefaultValuesToCar(data))).subscribe(
(response) => {
this.carListSubject$.next(response);
subscription.unsubscribe();
});
}
是否有任何方法可以防止对象在null时响应值?否则,我该如何使用此代码来帮助我呢?
谢谢!
I have this problem, I need the behaivorSubject not to return the value when it is null, since the result is grabbed by a component made by third parties and if this is null it throws me an error.
It should return the same only when I get the response from the server, I leave my code here:
getCarList(params): Observable<CarListGrid> {
if (this.carListSubject$ === undefined) {
this.carListSubject$ = new BehaviorSubject<CarListGrid>(null);
this.refetchCarList(params);
}
return this.carListSubject$.asObservable();
}
refetchCarList(gridParams) {
const subscription = this.http.post<CarListGrid>(this.baseUrl + 'GetCar', {gridParams} ,httpOptions).pipe(map(data => this.setDefaultValuesToCar(data))).subscribe(
(response) => {
this.carListSubject$.next(response);
subscription.unsubscribe();
});
}
Is there any way to prevent the subject from responding with a value when it is null? Or else, how can I give this code another way to help me with that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样过滤那些nulls:
You could filter those nulls out, like this: