使用cookie实现历史搜索记录功能
生活随笔
收集整理的這篇文章主要介紹了
使用cookie实现历史搜索记录功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在學習網易云音樂項目的時候,有一個歷史搜索記錄的功能,
就是你點擊的關鍵詞被記錄下來,然后顯示在搜索歷史記錄里;
主要實現方法:
項目中使用的是 'good-storage' :https://www.npmjs.com/package/good-storage
1、npm igood-storage
2、在組件中導入
import storage from 'good-storage';
3、我的搜索列表和搜索歷史列表的DOM結構
<!-- 熱門搜索 -->
<div class="block">
<p class="title">熱門搜索</p>
<div class="tags">
<el-button
class="btn"
size="mini"
v-for="(item, i) in searchHots"
:key="i"
@click="onClickHot(item)"
>{{item.first}}</el-button>
</div>
</div>
<!-- 搜索歷史 -->
<div class="block">
<p class="title">搜索歷史</p>
<div class="tags">
<el-button
class="btn"
size="mini"
v-for="(item, i) in searchHistorys"
:key="i"
>{{item.first}}</el-button>
</div>
</div>
4、點擊熱門搜索的每個button按鈕的時候,獲取它的關鍵詞(比如我點擊“六一快樂”,就把“六一快樂”這個關鍵詞保留下來)
methods部分:
onClickHot(item) {
const { first } = item;
this.goSearch(first);
},
goSearch(keyWords) {
this.searchHistorys.push({ first: keyWords });
storage.set(SEARCH_HISTORY_KEY, this.searchHistorys);
this.$router.push(`/search/${keyWords}`);
this.showSearchPane = false;
}
點擊一個熱門搜索,就把搜索的關鍵詞放到搜索列表數組searchHistorys中,然后把它存到cookie中
const SEARCH_HISTORY_KEY = '__search_history__';
data部分:
搜索歷史記錄從cookie中取出來
data() {
return {
searchValue: '',
showSearchPane: false,
searchHots: [],
searchHistorys: storage.get(SEARCH_HISTORY_KEY, []),
suggest: []
};
總結
以上是生活随笔為你收集整理的使用cookie实现历史搜索记录功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythonnone用法_python中
- 下一篇: 科立捷7代写频软件_天大厦大“两硕士论文