为axios成功分配全局变量

时间:2018-03-28 10:09:55

标签: javascript ajax laravel vue.js axios

你好伙计们这可以为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>

我搜索了很多并且对我如何做这件事感到困惑

1 个答案:

答案 0 :(得分:0)

试试这个

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