Angular 2 firebase如何将变量传递给另一个函数?

时间:2018-04-19 06:42:59

标签: angular typescript firebase firebase-storage

我有一个firebase存储功能,为每个上传的文件生成下载URL( upload.url = uploadTask.snapshot.downloadURL; ):

 pushUpload(upload: Project) {
    const storageRef = firebase.storage().ref();
    const uploadTask = storageRef.child(`${this.basePath}/${upload.file.name}`).put(upload.file);

    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot: firebase.storage.UploadTaskSnapshot) =>  {
        // upload in progress
        const snap = snapshot;
        upload.progress = (snap.bytesTransferred / snap.totalBytes) * 100;
      },
      (error) => {
        // upload failed
        console.log(error);
      },
      () => {
        // upload success
        if (uploadTask.snapshot.downloadURL) {
          upload.url = uploadTask.snapshot.downloadURL; //this is the variable
          upload.name = upload.file.name;

          this.fire.collection(`users/${this.auth.userId}/projects`).add( { photoURL: upload.url, file: upload.file.name, })
          this.saveFileData(upload);
          return;
        } else {
          console.error('No download URL!');
        }


      },

    );
  }

现在我想在urlPath:string:

的另一个函数中使用该变量
 public getZipFileContent(urlPath:string, pathInZip:string) {
    getFileContentFromRemoteZip(urlPath, pathInZip, (content) => {
      console.log(content);
    });
  }
}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

因此,您可以直接通过您的请求的 完整 来调用您的功能

this.urlPath= uploadTask.snapshot.downloadURL;

或者将其设置为某个局部变量,然后使用不带该参数的函数

public getZipFileContent(pathInZip:string) {
getFileContentFromRemoteZip(tis.urlPath, pathInZip, (content) => {
  console.log(content);
});

然后

bar_width

}

答案 1 :(得分:0)

抱歉,我自己回答得太快了:D

如果我想使用另一个不在我函数中的变量,我应该在另一个带有该变量的函数中调用该函数。

getUploads() {
    this.uploads = this.db.list(`profile/${this.auth.userId}/project`).snapshotChanges().map((actions) => {
      return actions.map((a) => {
        const data = a.payload.val();
        this.getZipFileContent(data.url, 'hello.html');
        const $key = a.payload.key;
        const $ref = a.payload.ref; 
        return { $key, ...data, $ref };
      });
    });
    return this.uploads;
  }