简体   繁体   English

将Firebase downloadURL传递给本地变量

[英]Passing firebase downloadURL to a local variable

I have this upload image in firebase that returning a downloadURL, how can I pass that value to my local variable image_url in vue js? 我在firebase中有此上传图像,该图像返回了一个downloadURL,如何将该值传递给vue js中的本地变量image_url?

//local variable
data(){
   return{
        image_url : '',
   }
}

button trigger to upload an image 按钮触发器以上传图像

<div class="col col-md-2 text-right pad-left">
     <md-button 
        class="btn-block 
        md-raised md-primary" 
        v-on:click="createOrUpdateUser()">Submit
     </md-button>
</div>

function to upload an image 上传图片功能

createOrUpdateUser(){
   // Create a root reference
   var storageRef = firebase.storage().ref('/images/' + 
   this.filename);      

   var uploadTask = storageRef.put(this.image);

   uploadTask.on('state_changed', function(snapshot){

        }, function(error) {

        // Handle unsuccessful uploads
        }, function() {

        // Handle successful uploads on complete
        // For instance, get the download URL: 
        https://firebasestorage.googleapis.com/...

        uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {

             //i need to pass downloadURL to the image_url variable above...
             console.log('File available at', downloadURL);
        });
   });
}
// ...
data() {
   image_url: ''
},
methods: {
  var self = this;
  createOrUpdateUser: function () {
      //... your code
      uploadTask.on('state_changed', function(snapshot){

      }, function(error) {

      // Handle unsuccessful uploads
      }, function() {

      // Handle successful uploads on complete
      // For instance, get the download URL: https://firebasestorage.googleapis.com/...

      uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
         //i need to pass downloadURL to the image_url variable above...
         self.image_url = downloadURL
         console.log('File available at', downloadURL);
       });
    });

    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM