代码之家  ›  专栏  ›  技术社区  ›  Angulandy2

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

  •  1
  • Angulandy2  · 技术社区  · 7 年前

    我有一个firebase storage witch函数,它为每个上传的文件生成下载url( 上载url=上载任务。快照下载URL; ):

     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 回复  |  直到 7 年前
        1
  •  1
  •   Stefan    7 年前

    因此,您可以直接从 完成 你这样的要求

    () => {
        // 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);
          this.getZipFileContent(upload.url, path);
          return;
        } else {
          console.error('No download URL!');
        }
    

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

    this.urlPath= uploadTask.snapshot.downloadURL;
    

    然后

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

    }

        2
  •  0
  •   Angulandy2    7 年前

    对不起,我自己回答得太快了: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;
      }
    
    推荐文章