代码之家  ›  专栏  ›  技术社区  ›  Stéphane GRILLON

订阅RxJs zip后,Angular 6 return observate<Car>

  •  0
  • Stéphane GRILLON  · 技术社区  · 6 年前

    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))
    );
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Stéphane GRILLON    6 年前

    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));
    
    推荐文章