带有Colcade异步问题的AngularFireStorage

时间:2020-08-10 18:45:39

标签: angular typescript asynchronous angularfire2

我在Firebase Storage中有一个图像文件夹,试图将其加载到Angular Web应用程序中,然后使用Colcade显示。我无法让Colcade的初始化代码仅在从Firebase Storage加载所有图像后才能运行。

我有一个服务,可以像这样检索所有图像的数组:

// Service
export class GalleryService {
  galleryList: String[];

  constructor(private afs: AngularFireStorage) {}

  async getGallery() {
    if (!this.galleryList) {
      let galleryList = [];
      const gallery = await this.afs.storage.ref('gallery').listAll();
      gallery.items.forEach(async (itemRef) => {
        galleryList.push(await itemRef.getDownloadURL());
      });

      this.galleryList = galleryList;
    }
    return this.galleryList;
  }
}

然后我从组件中调用以下代码:

// Component
export class MasonryComponent implements OnInit {
  galleryList: String[];

  constructor(private galleryService: GalleryService) {}

  ngOnInit(): void {
    this.getGallery();

    // This code is being run before the above finishes
    setTimeout(() => {
      if (this.galleryList) {
        this.galleryList.sort();
      }
      this.colcadeInit();
    }, 1000);
  }

  private async getGallery() {
    this.galleryList = await this.galleryService.getGallery();
  }

  private colcadeInit(): void {
    // selector string as first argument
    var colc = new Colcade('.grid', {
      columns: '.grid-col',
      items: '.grid-item',
    });
  }
}

我当前正在使用setTimeout来模拟代码应以其运行的顺序。但是,setTimeout的使用并不理想,因为图像的加载时间未知。请注意,此代码有效,Collcade或AngularFire的设置没有问题。

请帮助我,以便我的代码正常运行。谢谢。

0 个答案:

没有答案