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