RxJS combineLatest按非弃用语法调用被标记弃用问题咨询
原因说明
你触发弃用提示的核心原因是传参形式和你预期的未弃用签名不匹配:
- 你找到的未弃用签名要求第一个参数是所有Observable组成的数组,第二个参数才是投影函数;
- 你当前的写法是把4个Observable逐个作为独立参数传入,最后跟投影函数,这种重载本身就是RxJS官方标记的弃用重载,无论你是否传入可选的scheduler参数,只要匹配这个传参形式就会触发弃用提示。
修复方案
有两种合规的写法可以消除警告:
方案1:匹配你找到的未弃用签名
把所有Observable放到数组里作为第一个参数传入即可:
this.canBook$ = combineLatest( [ this.userSvc.canRedirect(this.fbSvc.getUserId()), this.userSvc.canBook(this.fbSvc.getUserId()), this.calSvc.segnaleOrarioIso8601(), this.selectedDay$ ], (canredir: boolean, canbook: boolean, iso8601: string, selday: ICGiorno) => { return canredir && (dayjs(selday.iso8601).diff(iso8601, 'hour') > 0) || canbook && (dayjs(selday.iso8601).diff(iso8601, 'hour') >= 24); } );
方案2:RxJS当前更推荐的pipe+map写法
官方现在更推荐把投影逻辑放到map操作符中处理,避免使用combineLatest的resultSelector参数:
import { map } from 'rxjs'; this.canBook$ = combineLatest([ this.userSvc.canRedirect(this.fbSvc.getUserId()), this.userSvc.canBook(this.fbSvc.getUserId()), this.calSvc.segnaleOrarioIso8601(), this.selectedDay$ ]).pipe( map(([canredir, canbook, iso8601, selday]: [boolean, boolean, string, ICGiorno]) => { return canredir && (dayjs(selday.iso8601).diff(iso8601, 'hour') > 0) || canbook && (dayjs(selday.iso8601).diff(iso8601, 'hour') >= 24); }) );
内容的提问来源于stack exchange,提问作者Lucio Crusca
相关产品推荐
相关产品推荐

