RxJS 6 switchMap使用不推荐使用的符号

Sub*_*med 7 rxjs angularfire2 angular angular5 angular6

我已更新Angular 5Angular 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;
  })
)
Run Code Online (Sandbox Code Playgroud)

我正在收到有关使用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;
})
Run Code Online (Sandbox Code Playgroud)

Error: Expected 1 argument but got 2.

siv*_*636 8

给定的作为第二个参数的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;
            })

            );
          }     
         )
        )
Run Code Online (Sandbox Code Playgroud)

  • 如果有人正在寻找@prolink007发布的迁移文档,它已被删除,可以在这里找到:https://github.com/ReactiveX/rxjs/commit/661a9a3cbc184fec0ca93178a2713b1044142b1f#diff-2059af00cf2f5c11dc2744fcf996a7348169fabf1c7cfc 00668db56482932611 (2认同)

Edu*_*rdo 5

.pipe(
  map(job => job[0]),
  switchMap((job) => {
    return job ? this.bookingService.findByID(job.property.id).pipe(
      map((bookings: Booking[]) => {
        return {
          job: job,
          bookings: bookings
        };
      })
    ) : Observable.empty();
  }
).subscribe((job, bookings: Booking[]) => {
  ...
})
Run Code Online (Sandbox Code Playgroud)