feat: 优化搜索功能,改进搜索历史管理和路由处理逻辑

This commit is contained in:
alger
2025-03-30 00:18:44 +08:00
parent 280fec1990
commit 477f8bb99b
2 changed files with 66 additions and 25 deletions
+24 -2
View File
@@ -105,7 +105,7 @@
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, watchEffect } from 'vue';
import { computed, onMounted, ref, watch, watchEffect } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
@@ -191,6 +191,18 @@ const isDark = computed({
// 搜索词
const searchValue = ref('');
// 使用 watch 代替 watchEffect 监听搜索值变化,确保深度监听
watch(
() => searchStore.searchValue,
(newValue) => {
if (newValue) {
searchValue.value = newValue;
}
},
{ immediate: true }
);
const search = () => {
const { value } = searchValue;
if (value === '') {
@@ -215,7 +227,17 @@ const search = () => {
const selectSearchType = (key: number) => {
searchStore.searchType = key;
if (searchValue.value) {
search();
if (router.currentRoute.value.path === '/search') {
search();
} else {
router.push({
path: '/search',
query: {
keyword: searchValue.value,
type: key
}
});
}
}
};