Skip to content

Commit 92ba62f

Browse files
committed
feat: add scroll-cache demo
1 parent 3a90f65 commit 92ba62f

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

src/pages/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const menuItems = computed(() => ([
1010
{ title: t('menus.unocssExample'), route: 'unocss' },
1111
{ title: t('menus.persistPiniaState'), route: 'counter' },
1212
{ title: t('menus.keepAlive'), route: 'keepalive' },
13+
{ title: t('menus.scrollCache'), route: 'scroll-cache' },
1314
{ title: t('menus.404Demo'), route: 'unknown' },
1415
]))
1516

src/pages/scroll-cache/index.vue

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<script setup lang="ts">
2+
defineOptions({
3+
name: 'ScrollCache',
4+
})
5+
6+
const list = ref([])
7+
const loading = ref(false)
8+
const finished = ref(false)
9+
const { t } = useI18n()
10+
11+
function onLoad() {
12+
setTimeout(() => {
13+
for (let i = 0; i < 10; i++) {
14+
list.value.push(`${list.value.length + 1}`)
15+
}
16+
17+
loading.value = false
18+
19+
if (list.value.length >= 40) {
20+
finished.value = true
21+
}
22+
}, 1000)
23+
}
24+
25+
const scrollTop = ref(0)
26+
27+
onActivated(() => {
28+
window.scrollTo(0, scrollTop.value)
29+
})
30+
31+
onBeforeRouteLeave(() => {
32+
scrollTop.value
33+
= window.scrollY
34+
|| document.documentElement.scrollTop
35+
|| document.body.scrollTop
36+
})
37+
</script>
38+
39+
<template>
40+
<van-list
41+
v-model:loading="loading"
42+
:finished="finished"
43+
finished-text="已经到底啦 ~"
44+
loading-text="加载中..."
45+
@load="onLoad"
46+
>
47+
<van-cell
48+
v-for="(item, index) in list"
49+
:key="index"
50+
:border="false"
51+
class="mb-8 rounded-12"
52+
>
53+
<template #title>
54+
{{ t('scrollCache.listItem') }}
55+
</template>
56+
57+
<template #value>
58+
{{ item }}
59+
</template>
60+
</van-cell>
61+
</van-list>
62+
</template>
63+
64+
<route lang="json5">
65+
{
66+
name: 'ScrollCache',
67+
meta: {
68+
title: '📜 ScrollCache',
69+
i18n: 'menus.scrollCache',
70+
keepAlive: true
71+
},
72+
}
73+
</route>

src/types/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare module 'vue' {
2323
VanForm: typeof import('vant/es')['Form']
2424
VanIcon: typeof import('vant/es')['Icon']
2525
VanImage: typeof import('vant/es')['Image']
26+
VanList: typeof import('vant/es')['List']
2627
VanNavBar: typeof import('vant/es')['NavBar']
2728
VanPicker: typeof import('vant/es')['Picker']
2829
VanPopup: typeof import('vant/es')['Popup']

src/types/typed-router.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare module 'vue-router/auto-routes' {
2828
'mock': RouteRecordInfo<'mock', '/mock', Record<never, never>, Record<never, never>>,
2929
'profile': RouteRecordInfo<'profile', '/profile', Record<never, never>, Record<never, never>>,
3030
'register': RouteRecordInfo<'register', '/register', Record<never, never>, Record<never, never>>,
31+
'ScrollCache': RouteRecordInfo<'ScrollCache', '/scroll-cache', Record<never, never>, Record<never, never>>,
3132
'settings': RouteRecordInfo<'settings', '/settings', Record<never, never>, Record<never, never>>,
3233
'unocss': RouteRecordInfo<'unocss', '/unocss', Record<never, never>, Record<never, never>>,
3334
}

0 commit comments

Comments
 (0)