11<template >
2- <div :key = " searchKeyword " class =" search" >
2+ <div class =" search" >
33 <div class =" title" >
44 <n-text class =" keyword" >{{ searchKeyword }}</n-text >
55 <n-text depth =" 3" >的相关搜索</n-text >
1717 <RouterView v-slot =" { Component }" >
1818 <Transition :name =" `router-${settingStore.routeAnimation}`" mode =" out-in" >
1919 <KeepAlive v-if =" settingStore.useKeepAlive" >
20- <component :is =" Component" :keyword =" searchKeyword" class =" router-view" />
20+ <component
21+ :is =" Component"
22+ :key =" route.fullPath"
23+ :keyword =" searchKeyword"
24+ class =" router-view"
25+ />
2126 </KeepAlive >
22- <component v-else :is =" Component" :keyword =" searchKeyword" class =" router-view" />
27+ <component
28+ v-else
29+ :is =" Component"
30+ :key =" route.fullPath"
31+ :keyword =" searchKeyword"
32+ class =" router-view"
33+ />
2334 </Transition >
2435 </RouterView >
2536 </div >
2839<script setup lang="ts">
2940import { useSettingStore } from " @/stores" ;
3041
42+ const route = useRoute ();
3143const router = useRouter ();
3244const settingStore = useSettingStore ();
3345
3446// 搜索关键词
35- const searchKeyword = computed (() => router . currentRoute . value .query .keyword as string );
47+ const searchKeyword = computed (() => route .query .keyword as string );
3648
3749// 搜索分类
38- const searchType = ref <string >(( router . currentRoute . value ?. name as string ) || " search-songs" );
50+ const searchType = ref <string >(" search-songs" );
3951
4052// Tabs 改变
4153const tabChange = (value : string ) => {
@@ -47,10 +59,16 @@ const tabChange = (value: string) => {
4759 });
4860};
4961
50- onBeforeRouteUpdate ((to ) => {
51- if (to .matched [0 ].name !== " search" ) return ;
52- searchType .value = to .name as string ;
53- });
62+ // 监听路由变化,同步 Tab 状态
63+ watch (
64+ () => route .name ,
65+ (name ) => {
66+ if (name && name .toString ().startsWith (" search-" )) {
67+ searchType .value = name as string ;
68+ }
69+ },
70+ { immediate: true },
71+ );
5472 </script >
5573
5674<style lang="scss" scoped>
0 commit comments