使用vue制作加載更多功能,通過ajax獲取的數據往data里面push經常不成功,原因是push是往數組中追加數據內容的,而不能用作數組之間的拼接,ajax獲取的數據就是數組形式的,因此不成功,應該使用concat()拼接兩個數組。
//這是錯誤的寫法$.ajax({ type:'get', async:false, url:path+'no/noticeMobile/getSendNoticeList?imToken='+ getToken +'&pageFlag=2', dataType: "json", success: function(msg){ _self.$set('loadMore', msg); _self.conList.push(_self.loadMore); } });
//這是正確的寫法$.ajax({ type:'get', async:false, url:path+'no/noticeMobile/getSendNoticeList?imToken='+ getToken +'&pageFlag=2', dataType: "json", success: function(msg){ _self.$set('main',_self.main.concat(msg)) } });
模擬ajax數據加載測試效果如下:
總結:還是自己js基礎知識不扎實,push和concat兩個函數用法沒有搞清楚,如果你有更好的方法歡迎討論。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答