angular - RxJS 6 switchMap 不推荐使用的符号
问题描述
我已经从 更新Angular 5
到Angular 6
。现在我正在尝试更新我的代码以使其与RxJS 6
.
.pipe(
map(job => job[0]),
switchMap((job) => {
return job ? this.bookingService.findByID(job.property.id) : Observable.empty();
}, (job: Job, bookings: Booking[]) => {
this.mark_jobs_unavailable(job, bookings);
return job;
})
)
我收到关于使用 switchMap 的警告Deprecated Symbol is used
。
这些是我的进口:import {map, switchMap} from 'rxjs/operators';
有没有其他方法可以在 v6 中使用 switchMap?此外,如果我不更改我的代码rxjs-compat
应该使我现有的代码工作(我已经安装)但我得到以下错误,使用相同的代码但在 RxJS 5 样式:
.map(job => job[0])
.switchMap((job) => {
return job ? this.bookingService.findByID(job.property.id) : Observable.empty();
}, (job: Job, bookings: Booking[]) => {
this.mark_jobs_unavailable(job, bookings);
return job;
})
Error: Expected 1 argument but got 2
.
解决方案
作为第二个参数给出的 resultSelector 函数switchMap
已弃用。您需要删除它并使用map
运算符实现目标。
这里最棘手的部分是决定将map
操作符放在哪里。实际上 map 操作符进入作为参数提供的函数体内部switchMap
。
没有结果选择器功能的代码将类似于以下内容:
.pipe(
map(job => job[0]),
switchMap((job) => {
return (job ? this.bookingService.findByID(job.property.id) : Observable.empty()).pipe(
// This is the mapping function provided as the alternative to the deprecated result selector function
// This should be placed inside the body of the function which is the 1st (and only one) argument of switchMap
map((bookings: Booking[])=>{
this.mark_jobs_unavailable(job, bookings);
return job;
})
);
}
)
)
推荐阅读
- email - 从电子邮件链接更新日历
- java - 如何在 android 通话期间以编程方式拨打 #1?
- java - println() 与 Printwriter 对象不写入文件
- excel - Power Query Table.Group List.Sum(两列)
- c# - 如果计时器自行启动,过去运行的代码是否会被垃圾收集?
- google-cloud-platform - 如何在 Google Cloud Platform 中使用 python3.5 制作虚拟机实例
- java - 从firebase读取数据时如何在android应用程序中添加延迟
- linux-kernel - 如何使 cpuset 的 cpuset.cpu_exclusive 功能正常工作
- java - 从 Swing JFrame 调用 SWT 主应用程序窗口
- javascript - 如何在 gridstack.js 中获取新的 y 值