javascript - Laravel vue 无限滚动不会拉新数据
问题描述
我正在使用元素 UI,它具有无限滚动,但我无法让它在我的应用程序中工作。
代码
script
data() {
return {
count: '', // default is 8
total: '', // defalt is 15
perPage: '', // default is 8
loading: false,
products: [],
}
},
computed: {
noMore () {
return this.count >= this.total // if this.count (8) reach this.total (15) then show no more text.
},
disabled () {
return this.loading || this.noMore
}
},
methods: {
load () {
this.loading = true
setTimeout(() => {
this.count += this.perPage // add 8 more to page
this.loading = false
}, 1000)
},
getProducts: function(){
axios.get('/api/products').then(response => {
this.products = response.data.data;
this.count = response.data.meta.per_page; // set count to 8 (backend based)
this.perPage = response.data.meta.per_page; // set perPage to 8 (backend based)
this.total = response.data.meta.total; // my testing data are 15 so it sets total to 15 (backend based)
}).catch((err) => console.error(err));
}
},
mounted() {
this.getProducts();
}
HTML
<div class="row mt-5 my-5 infinite-list" v-infinite-scroll="load" infinite-scroll-disabled="disabled"> // load function
<div class="col-md-3 mb-3" v-for="product in products" :key="product.slug" :offset="1"> // loop function
<el-card shadow="hover" :body-style="{ padding: '0px' }">
{{product.name}}
</el-card>
</div>
// infinite functions
<div class="col-md-12 mt-3" v-if="loading">
<el-card shadow="always" class="text-center">Loading...</el-card>
</div>
<div class="col-md-12 mt-3" v-if="noMore">
<el-card shadow="always" class="text-center">That's it, No more!</el-card>
</div>
</div>
Meta data
Result
我的代码做错了什么?
解决方案
它是因为你的加载功能看看它,
load () {
this.loading = true
setTimeout(() => {
this.count += this.perPage // add 8 more to page
this.loading = false
}, 1000)
},
您已经设置了 1000 毫秒的超时时间,在每次滚动后调用第一次计数通过 fetch 调用更新,然后第二次通过加载函数的超时回调更新它,而不是在 1000 毫秒后使 this.loading =false它在 fetch 调用成功时为假,并且同样适用于更新 this.count,希望这会对您有所帮助。
编辑:
您的加载功能应如下所示
load () {
this.loading = true
this.getProducts();//previously you getproduct is called once only after mounted,you you need to call it each time whenever load is called
},
你的 fetch 函数应该是这样的
getProducts: function(){
axios.get('/api/products').then(response => {
this.products = response.data.data;
this.count += this.perPage // add 8 more to page
this.loading = false
this.perPage = response.data.meta.per_page; // set perPage to 8 (backend based)
this.total = response.data.meta.total; // my testing data are 15 so it sets total to 15 (backend based)
}).catch((err) => console.error(err));
}
您可以尝试的另一件事是,使用 getProducts 而不是加载和删除加载函数,在您的 getProducts 函数中设置 this.loading=true,以前您的 getProducts 在安装组件时只调用一次。每次加载时都需要调用 getProducts 函数要求更多。
推荐阅读
- sql - SQL 帮助:异常报告 - 扩展 2
- html - 为什么我的 CSS 不能在索引以外的其他页面上工作?
- git - Git rebase 冲突
- bash - 难以在 bash 中使用 readlink 混合变量和字符串来返回绝对路径
- javascript - 错误:无法解析 LoginPage 的所有参数:([object Object]、[object Object]、[object Object]、?)
- c++ - 我的代码,有时它说在抛出“std::bad_alloc”what() 的实例后调用终止:std::bad_alloc。然后有时它不会
- linux - 使用带有 FSELECT 的对话框的基本文件资源管理器:Bash 和循环?
- php - 在 null 上调用成员函数 attach()
- reactjs - Firebase Auth,在从反应客户端登录用户后,如何验证用户在我的其他 api 中是否合法?
- web-scraping - Jsoup 超时并且 cURL 仅适用于“--compressed”标头 - 如何在 Jsoup 中模拟此标头?