Angular firebase从snapshotChanges()第二次订阅Observable不起作用

时间:2018-03-28 16:08:56

标签: angular firebase firebase-realtime-database

我有upload.module提供的upload.service。当我在upload.component中使用它时,它工作正常:

export class UploadsListComponent implements OnInit {

  uploads: Observable<Upload[]>;
  showSpinner = true;

  constructor(private upSvc: UploadService) { }

  ngOnInit() {
    this.uploads = this.upSvc.getUploads();
    console.log("upload list upload :")
    this.uploads.subscribe(() => this.showSpinner = false);
  }
}

Upload.component.html:

<div *ngFor="let upload of uploads | async">
  <upload-detail [upload]='upload'></upload-detail>
  <img src="{{upload.url}}" alt="">
</div>

当我在另一个模块组件中使用它时,upload.url为null。

我觉得这个功能不好:

 uploads: Observable<Upload[]>;

getUploads() {
    this.uploads = this.db.list(`${this.auth.userId}`).snapshotChanges().map((actions) => {
      return actions.map((a) => {
        const data = a.payload.val();
       const $key = a.payload.key;
       return { $key, ...data };
      });

    });
    return this.uploads;
  }

为什么upload.url只显示一次?

照片说明: enter image description here

注意:不要看控制台它有一些无关紧要的错误和一些不好的console.log

0 个答案:

没有答案