From bcf7375b76e2e666ce717c610a83626f9d9d6fc4 Mon Sep 17 00:00:00 2001 From: XiaoMo Date: Thu, 6 Apr 2023 15:00:13 +0800 Subject: [PATCH] =?UTF-8?q?a1300399510@qq.com=20=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E4=BA=8E=202023/04/06=20-15:00:01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/SearchBox.vue | 1 + src/store/index.js | 3 ++- .../search/searchResult/SearchResult.vue | 20 +++++++++++-------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/SearchBox.vue b/src/components/SearchBox.vue index 57baea1..a349247 100755 --- a/src/components/SearchBox.vue +++ b/src/components/SearchBox.vue @@ -53,6 +53,7 @@ export default { watch: { historicalSearch(val, oldval) { + console.log("val", val); if (val.length > 10) this.historicalSearch.slice(0, 10) this.$store.commit('setHistoricalSearch', val) } diff --git a/src/store/index.js b/src/store/index.js index fdb3f59..2273ed8 100755 --- a/src/store/index.js +++ b/src/store/index.js @@ -11,6 +11,7 @@ export default new Vuex.Store({ }, mutations: { setHistoricalSearch(state, payload) { + payload = [...new Set(payload)] state.historicalSearch = payload localStorage.setItem('historicalSearch', JSON.stringify(payload)); @@ -24,7 +25,7 @@ export default new Vuex.Store({ let historicalSearch = JSON.parse(localStorage.getItem('historicalSearch')) || [] commit('setHistoricalSearch', historicalSearch) }, - + }, modules: { } diff --git a/src/views/search/searchResult/SearchResult.vue b/src/views/search/searchResult/SearchResult.vue index 0b2bb03..3416152 100755 --- a/src/views/search/searchResult/SearchResult.vue +++ b/src/views/search/searchResult/SearchResult.vue @@ -4,7 +4,7 @@
-
@@ -14,7 +14,6 @@
大约找到
{{ count }}
条结果
-
@@ -98,10 +97,9 @@ export default { historicalSearch(val, oldval) { if (val.length > 10) this.historicalSearch.slice(0, 10) this.$store.commit('setHistoricalSearch', val) - } + }, }, - methods: { // 处理点击取消的返回上一页 handCancel() { @@ -109,6 +107,15 @@ export default { else this.$router.push('/recommend') }, + // 处理搜索框回车 + handleSearchResult() { + let kw = this.kw + // this.historicalSearch.push(kw) + if (!this.historicalSearch.includes(kw)) this.historicalSearch.unshift(kw) + + this.getSearchResult() + }, + // 获取搜索结果数据 getSearchResult() { if (!this.kw) return @@ -123,7 +130,6 @@ export default { }).then(res => { let data = res.data - this.list = data.data this.count = data.count this.limit = data.limit @@ -132,9 +138,7 @@ export default { document.body.scrollTop = 0; this.searchResultState = false }).finally(() => { - let kw = this.kw - this.historicalSearch.push(kw) - if (!this.historicalSearch.includes(kw)) this.historicalSearch.unshift(kw) + }) },