xtraderClient/docs/composables/useSearchHistory.md
2026-02-14 18:59:36 +08:00

38 lines
943 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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` 常量