带有Colcade异步问题的AngularFireStorage

问题描述

我在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的设置没有问题。

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)