feat: 修复无限加载页面的错误处理逻辑,优化数据加载和缓存更新
This commit is contained in:
@ -31,18 +31,19 @@ const list = shallowRef<Record<string, never>[]>(lastCache.list);
|
||||
const loadData = async (page: number) => {
|
||||
if (!lastCache.hasMore) return { hasMore: false };
|
||||
|
||||
const newPage = page + lastCache.page;
|
||||
await new Promise((resolve) => setTimeout(resolve, 250 + 1000));
|
||||
if (page === 2 && isPage2ErrorVisible) {
|
||||
if (newPage === 2 && isPage2ErrorVisible) {
|
||||
isPage2ErrorVisible = false;
|
||||
throw new Error('Failed to load comments. Because page === 2');
|
||||
}
|
||||
const response = await fetch(
|
||||
`https://jsonplaceholder.typicode.com/comments?_page=${page + lastCache.page}&_limit=1×tamp=${Date.now()}`,
|
||||
`https://jsonplaceholder.typicode.com/comments?_page=${newPage}&_limit=1×tamp=${Date.now()}`,
|
||||
);
|
||||
const data = await response.json();
|
||||
list.value = list.value.concat(data);
|
||||
const hasMore = list.value.length < 5;
|
||||
updateCache({ page: page + lastCache.page, list: list.value, hasMore });
|
||||
updateCache({ page: newPage, list: list.value, hasMore });
|
||||
return { hasMore };
|
||||
};
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user