Angular 6 return Observable<Car> 在订阅 RxJs zip 之后

Angular 6 return Observable<Car> after a subscribe of RxJs zip

我想要return一个Observable<Car>

import { zip, Observable } from 'rxjs';

public createMedia(car: Car, dataUrl: string): Observable<Car> {
    const url = this.carDataService.createCar(car);
    const blob = this.dataUrlToBlob(dataUrl);
    const z = zip(url, blob);
    z.subscribe(val => this.saveData(val));
    return ...???(I want return Observable<Car> from saveData(val))
}

saveData(val): Observable<Car> {
  console.log('saveData => val 0: ' + val[0].id + ' val 1: ' + val[1]);
  return this.mediaDataService.saveBytes(val[0].id, val[1]);
}

const oMedia: Observable<Car> = this.createMedia(media);
oMedia.pipe(
  tap(media => console.log('Media created with id: ' + media.id)),
  map(media => this.setMediaId(step, media))
);

管道到 zip 结果并使用 flatMap 运算符:

import { flatMap } from 'rxjs/operators';
import { zip, Observable } from 'rxjs';

public createMedia(car: Car, dataUrl: string): Observable<Car> {
  const url = this.carDataService.createCar(car);
  const blob = this.dataUrlToBlob(dataUrl);

  return zip(url, blob).pipe(
    flatMap(val => this.saveData(val))
  );
}

并且不要忘记订阅 createMedia 结果。

oMedia.subscribe(car => console.log('Media created with id: ' + car.id));