Skip to content

Commit

Permalink
fix: 触底刷新请求问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Plumbiu committed Feb 4, 2023
1 parent e0d95a0 commit 0d95c58
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2023-02-03T11:55:26.798Z"
"x-generation-date": "2023-02-04T03:55:33.793Z"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/ArticlesList/Item/AuthorInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defineProps({
}
.author_panel::after {
border: 10px solid transparent;
@apply w-0 h-0 border-[10px]
@apply w-0 h-0 border-[10px] border-t-white
@apply absolute bottom-0 left-1/2 content-none -translate-x-1/2 translate-y-full
@apply dark:border-t-jj_bg_gray
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/ArticlesList/Item/Topbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defineProps({
@apply transition text-jj-font px-3 border-r-1 pl-0 hover:text-[#1E80FF]
}
.author_info {
@apply scale-0 delay-150 transition-all
@apply scale-0 delay-150 transition-all text-black
}
.author_info:hover {
@apply scale-100;
Expand Down
17 changes: 10 additions & 7 deletions frontend/components/ArticlesList/index.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<!-- eslint-disable no-console -->
<script setup lang="ts">
const route = useRoute()
let pagenum = 1
const isLoading = useState('isLoading', () => false)
const isEmpty = useState('isEmpty', () => false)
const artlistData = useArtlist([])
const artlistData = useArtlist(await useFetchPostData())
const addArtListItem = () => {
if (useScrollBottom()) {
pagenum++
Expand All @@ -14,7 +15,6 @@ const addArtListItem = () => {
}
watch(route, () => {
pagenum = 1
artlistData.value = []
isLoading.value = true
useFetchPostData(route.path, route.query?.sort).then((data) => {
if (!data.length) {
Expand All @@ -25,21 +25,24 @@ watch(route, () => {
isEmpty.value = false
isLoading.value = false
})
}, { deep: true, immediate: true })
onMounted(() => {
}, { deep: true })
const bottomHandler = useThrottle(addArtListItem)
onBeforeMount(() => {
console.log('onMounted')
const EmployeeWindow = window as any
EmployeeWindow.addEventListener('scroll', useThrottle(addArtListItem))
EmployeeWindow.addEventListener('scroll', bottomHandler)
})
onUnmounted(() => {
console.log('onUnmounted')
const EmployeeWindow = window as any
EmployeeWindow.removeEventListener('scroll', useThrottle(addArtListItem)) // 页面离开后销毁监听事件
EmployeeWindow.removeEventListener('scroll', bottomHandler)
})
</script>

<template>
<div class="articlelist">
<ArticlesListNavigation />
<ArticlesListSkeleton v-if="isLoading && isEmpty" />
<ArticlesListSkeleton v-if="isLoading || isEmpty" />
<ul v-else>
<ArticlesListItem
v-for="item in artlistData"
Expand Down
4 changes: 2 additions & 2 deletions frontend/composables/useThrottle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default (fn: () => any, wait = 1000) => {
export default (fn?: () => any, wait = 1000) => {
let timer: NodeJS.Timeout | null = null
return () => {
if (!timer) {
timer = setTimeout(() => {
timer = null
fn()
fn && fn()
}, wait)
}
}
Expand Down

0 comments on commit 0d95c58

Please sign in to comment.