How to load images async with RxJs and perform a method when all loaded

前端 未结 6 911
臣服心动
臣服心动 2020-12-30 10:25

I am trying to convert my promise based code to RxJs but have a hard time to get my head around Rx especially RxJs.

I have a an array with paths.

var         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 10:53

    Here is the Angular / Typescript version to load an Image with RxJS:

    import { Observable, Observer } from "rxjs"; 
    
    public loadImage(imagePath: string): Observable {
      return Observable.create((observer: Observer) => {
        var img = new Image();
        img.src = imagePath;
        img.onload = () => {
          observer.next(img);
          observer.complete();
        };
        img.onerror = err => {
          observer.error(err);
        };
      });
    }
    

提交回复
热议问题