943 B
943 B
useSearchHistory.ts
路径:src/composables/useSearchHistory.ts
功能用途
搜索历史组合式函数,将关键词持久化到 localStorage(键 polyclient_search_history),最多保留 10 条,去重且新词置顶。
核心能力
list:只读的搜索历史数组add(keyword):添加关键词,去重并限制数量remove(index):删除指定索引clearAll:清空全部
使用方式
import { useSearchHistory } from '@/composables/useSearchHistory'
const searchHistory = useSearchHistory()
// 提交搜索时
searchHistory.add(searchKeyword)
// 展示历史
const searchHistoryList = searchHistory.list.value
// 清空
searchHistory.clearAll()
扩展方式
- 分类历史:扩展为
useSearchHistory(category),按分类存储 - 同步服务端:在
add时调用接口同步到后端 - 最大条数:修改
MAX_HISTORY常量