【问题标题】:assign global variable on axios success在 axios 成功时分配全局变量
【发布时间】:2018-03-28 10:09:54
【问题描述】:

大家好,可以为 axios 成功赋值,这样我就可以将该变量用于其他函数,例如:-

<script>

var x ="";
export default {
  methods:{
  getshop(){
    this.$http.get('http://localhost:3000/api/shopestablishments') .then(function (response) {        
    return x= response.data;//how i can assign value of x from here
      })
  },

  onClick(){       
    // how I can use assign variables of x over here
             },
    },

}
</script>

我搜索了很多,很困惑我怎么能做这件事

【问题讨论】:

标签: javascript ajax laravel vue.js axios


【解决方案1】:

试试这个

getshop() {
  this.$http.get('http://localhost:3000/api/shopestablishments') 
    .then(function (response) {
      // Save response.data in variable `x`         
      var x = response.data;
      // Call second function with response.data
      this.yourFunction(x);
    })
}

【讨论】:

  • 它在函数 onclick 中抛出未定义变量
  • then 块中调用你的函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多