等待观察的完成 - 角(Waiting for observable to finish - Ang

2019-10-30 08:48发布

我使用的角度和火力地堡存储上传图片,我有这个问题在我的urlImg它是undefined ,当我尝试打印它在console.log的内部tap

 this.snapshot = this.snapshot = this.task.snapshotChanges().pipe(
      finalize(() => {
        this.storage
          .ref(path)
          .getDownloadURL()
          .subscribe(url => {
            this.urlImg = url; // with this you can use it in the html
          });
      }),
      tap(snap => {
        if (snap.bytesTransferred === snap.totalBytes) {
          // Update firestore on completion
          //Code to upload on completion
          console.log("url");
          console.log(this.urlImg);
        }
      })
    );

Answer 1:

一种选择是采取订阅的完整部分的优势。 然后,你可以肯定的是this.urlImg有一个值。

.subscribe(
   url => this.urlImg = url,
   err => console.error('Request for URL failed: ' + err),
   () => {
      // operations when URL request is completed
   }
)


文章来源: Waiting for observable to finish - Angular