From 2c5462cd9783b64b566ce45b2af2babd20744488 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Sat, 16 Aug 2025 16:19:44 +0800 Subject: [PATCH] feat: persist home tab selection --- frontend_nuxt/pages/index.vue | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/frontend_nuxt/pages/index.vue b/frontend_nuxt/pages/index.vue index bb8eee891..18d9e835c 100644 --- a/frontend_nuxt/pages/index.vue +++ b/frontend_nuxt/pages/index.vue @@ -147,9 +147,20 @@ const categoryOptions = ref([]) const isLoadingMore = ref(false) const topics = ref(['最新回复', '最新', '排行榜' /*, '热门', '类别'*/]) -const selectedTopic = ref( - route.query.view === 'ranking' ? '排行榜' : route.query.view === 'latest' ? '最新' : '最新回复', -) +const HOME_TAB_KEY = 'homeTab' +const routeDefault = + route.query.view === 'ranking' ? '排行榜' : route.query.view === 'latest' ? '最新' : null +const selectedTopic = ref(routeDefault || '最新回复') +if (import.meta.client) { + if (routeDefault) { + localStorage.setItem(HOME_TAB_KEY, routeDefault) + } else { + const stored = localStorage.getItem(HOME_TAB_KEY) + if (stored && topics.value.includes(stored)) { + selectedTopic.value = stored + } + } +} const articles = ref([]) const page = ref(0) const pageSize = 10 @@ -340,7 +351,8 @@ watch( watch([selectedCategory, selectedTags], () => { loadOptions() }) -watch(selectedTopic, () => { +watch(selectedTopic, (val) => { + if (import.meta.client) localStorage.setItem(HOME_TAB_KEY, val) // 仅当需要额外选项时加载 loadOptions() })