【发布时间】:2020-12-05 02:56:02
【问题描述】:
我正在尝试在我的 vue chrome 扩展中实现无限滚动。我有这段代码,但问题是当到达页面底部时,用于获取新数据的 ajax 调用被多次触发。我该如何解决?
mounted() {
this.$store.commit('preloader', false)
window.onscroll = () => {
if( window.scrollY + window.innerHeight >= document.body.scrollHeight ){
console.log('fired')
this.nextPageLoaded = false
this.nextPage()
}
}
},
methods: {
nextPage() {
console.log('Loading next page')
axios.get('https://localhost:8080/api/media')
.then( (response) => {
console.log(response.data)
this.$store.commit('profileMedia', response.data.media)
this.nextPageLoaded = true
})
}
}
我尝试在加载数据后将变量 nextPageLoad 设置为 true,并在滚动事件到达底部但无法按预期工作时设置为 false。任何解决方案都会受到赞赏
【问题讨论】:
-
是否可以为相同的创建jsfiddle或codesandbox?
-
不幸的是,我使用的数据是由我的本地开发服务器提供的,我无法在小提琴上复制相同的结构
标签: javascript ajax vue.js infinite-scroll