【问题标题】:Passing firebase downloadURL to a local variable将 firebase downloadURL 传递给本地变量
【发布时间】:2018-12-23 00:32:27
【问题描述】:

我在firebase中有这个上传图片返回一个downloadURL,我怎样才能将该值传递给我在vue js中的本地变量image_url?

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

按钮触发上传图片

<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>

上传图片的功能

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);
        });
   });
}

【问题讨论】:

    标签: javascript firebase vue.js firebase-storage


    【解决方案1】:
    // ...
    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);
           });
        });
    
        }
      }
    

    【讨论】:

    • 好吧,让我检查一下。同时,您可以在问题中添加完整的代码吗?
    • @Dhenz 我已经更新了我的答案。这对我有用。
    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2012-05-16
    • 2017-01-08
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多