2017-08-28 156 views
2

我使用axiosvue.js來獲取數據爲無限分頁。它工作正常,預計在沒有數據來呈現:如何處理axios中的空響應?

fetchData() { 
    this.loading = true 
    this.page++; 
    axios.get(this.BASE_URL + '/api/jokes/'+'?page='+this.page).then( 
    response => 
    //how to exist if there is no data in the response? 
     this.jokes = response.data).catch(function (error) { 
     console.log(error); 
     }); 

我不知道如何停止渲染當我們到達最後一頁,也沒有更多的數據,以顯示?我看過docs但找不到我的答案。

回答

1

也許介紹一些基本的標誌邏輯。我採取了自由承擔,但你總是可以定義你的邏輯

fetchData() { 
    this.loading = true; 
    if (this.page > 0) { 
     axios.get(this.BASE_URL + '/api/jokes/page=' + this.page) 
      .then(response => { 
       const isDataAvailable = response.data && response.data.length; 
       this.jokes = isDataAvailable ? response.data : []; 
       this.page = isDataAvailable ? (this.page + 1) : 0; 
      }) 
      .catch(function(error) { 
       console.error(error); 
      }); 
    } 
} 
+0

這是行不通的。我不想重寫整個功能。當response.data爲空時,請限制您的答案以打破此功能。 – Karlom