2017-09-15 84 views
0

我想立即向DB提出请求。问题是:我无法清除以前的ajax请求的结果,即使我清除包含它的数组时,当我输入更快时也不起作用。Vuejs无法清除之前ajax的结果

第二个(较小的:D)问题是我必须使嵌套循环,使其工作。

我想很多时间来解决这些问题......

Click to see an image

组件1:HTML

<input v-model="invoice_number" type="text" name="invoice_number" class="form-control" id="" placeholder="Invoice number" @keyup="max_invoice_number" required> 

组件1:JavaScript的

max_invoice_number: function() { 

     axios.get('/max_invoice_number/' + this.invoice_number) 
     .then(response => this.max_invoice_response.push({ 
      info: response.data 

     })); 

     Event.$emit('max_invoice_response', this.max_invoice_response); 
    }, 

组件2 HTML

<div v-for="a in max_invoice_response" class="bg-success col-xs-12"> 
      <div v-for="b in a">{{b.info}}</div> 
     </div> 

元器件2的Javascript

data(){ 
     return{ 

      ref_numbers: [], 
      max_invoice_response: [] 
     } 
    }, 

    created: function(){ 

     Event.$on('max_invoice_response', function(response) { this.clear; this.max_invoice_response.push(response); }.bind(this)); 

    }, 

    methods: { 
     clear: function() { 
      this.max_invoice_response.splice(0); 
     } 

    }, 
} 
+1

难道你不会在你创建的方法中忘记'this.clear'上的圆括号吗? (应该是'this.clear()') –

+0

谢谢你的回答,但它不是:) – Flash

回答

1

从丢失的括号确定开,从而假设清楚方法被调用,并在组件2中的阵列被清空。

我每次进行AJAX调用时,都会将组件1的响应数据存储在this.max_invoice_response(组件1上)中。下一步是您将this.max_invoice_response作为有效负载发出。此字段包含迄今收到的全部回复。

因此,在处理组件2中的事件时,您也会收到所有响应。在清除数组的同时,它仍将所有响应推送到组件2的this.max_invoice_response。因此,您还需要清除组件1,或者只是覆盖已存储的数据。

但要小心! HTTP响应被处理为异步。由于每触发一个键,第二个字母的响应可能早于第一个字母的响应。

+0

谢谢trieng帮助我,但这不是因为我有组件1上的那行,但我删除它当我在这里复制时,我错了。在这里你可以看到代码https://github.com/iliyan940/euromedic_v2/tree/master/resources/assets/js/components – Flash

+0

我看到了,更好地更新你的问题,以便其他人也知道。 如果您慢速键入(每5秒一次键入),是否会发生相同的错误? –

0

已解决!去抖是我需要的功能。 这是我的代码,如果有人在相同的情况。

max_invoice_number: _.debounce(function() { 

     this.max_invoice_response = []; 

     axios.get('/max_invoice_number/' + this.invoice_number) 
     .then(response => this.max_invoice_response.push({ 
      info: response.data 

     })); 

     Event.$emit('max_invoice_response', this.max_invoice_response); 
    },300),