如何在axios.get外部调用变量?它没有给我任何结果

问题描述 投票:0回答:1

this is my sample code and i need to pass the data inside the axios.get.

我需要从console.log(this.var1)的位置获取this.var1的值

newModalMultiple(){
    this.var1=0;
      for( i = 0; i < this.checkedNames.length; i++)
      {
          axios.get('/api/getjvlfilter', {params: {JVLHDRID: this.checkedNames[i]}})
            .then(response => {
              this.jvlfilter = response.data;
                  this.var1=this.jvlfilter[0].BillAmount; 
              })
      }
     console.log(this.var1);
  }
laravel axios vue-component
1个回答
0
投票

使用另一个变量var vm = this;,并在get函数中将this替换为vm

 newModalMultiple(){
   var vm = this;
   vm.var1=0;
  for( i = 0; i < this.checkedNames.length; i++)
  {
      axios.get('/api/getjvlfilter', {params: {JVLHDRID: vm.checkedNames[i]}})
        .then(response => {
          vm.jvlfilter = response.data;
              vm.var1= vm.jvlfilter[0].BillAmount; 
          })
  }
  console.log(vm.var1);
}
© www.soinside.com 2019 - 2024. All rights reserved.