hookehuyr

完善搜索的相关逻辑

1 <!-- 1 <!--
2 * @Date: 2024-09-26 13:42:11 2 * @Date: 2024-09-26 13:42:11
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-10-20 13:01:08 4 + * @LastEditTime: 2024-10-20 18:49:12
5 * @FilePath: /hager/src/components/common/hagerHeader.vue 5 * @FilePath: /hager/src/components/common/hagerHeader.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
21 <el-col :span="24"> 21 <el-col :span="24">
22 <div class="search"> 22 <div class="search">
23 <i class="el-icon-search" style="color: #00477E;"></i>&nbsp; 23 <i class="el-icon-search" style="color: #00477E;"></i>&nbsp;
24 - <input placeholder="搜索" /> 24 + <input placeholder="搜索" v-model="keyword" @blur="goToSearch" @keydown.enter="goToSearch" />
25 </div> 25 </div>
26 </el-col> 26 </el-col>
27 </el-row> 27 </el-row>
...@@ -289,6 +289,13 @@ export default { ...@@ -289,6 +289,13 @@ export default {
289 show_menu: false, 289 show_menu: false,
290 margin_top: 0, 290 margin_top: 0,
291 // choose_country: false, // 主动选择了国家地区 291 // choose_country: false, // 主动选择了国家地区
292 + keyword: '',
293 + }
294 + },
295 + watch: {
296 + // 监听路由参数变化时,更新输入框的值
297 + '$route.query.keyword': function(newKeyword) {
298 + this.keyword = newKeyword || '';
292 } 299 }
293 }, 300 },
294 mounted () { 301 mounted () {
...@@ -449,6 +456,24 @@ export default { ...@@ -449,6 +456,24 @@ export default {
449 this.show_menu = false; 456 this.show_menu = false;
450 $('body').css('overflow', 'auto'); 457 $('body').css('overflow', 'auto');
451 }, 458 },
459 + goToSearch () {
460 + this.updateURL()
461 + },
462 + updateURL() {
463 + const currentKeyword = this.$route.query.keyword;
464 +
465 + // 如果输入的 keyword 和当前路由的 keyword 相同,不做更新
466 + if (this.keyword === currentKeyword) return;
467 +
468 + // 使用 Vue Router 动态更新 URL 中的 keyword 参数
469 + this.$router.replace({
470 + path: '/search', // 确保路径不变
471 + query: {
472 + ...this.$route.query, // 保留其他可能存在的 query 参数
473 + keyword: this.keyword // 更新 keyword 参数
474 + }
475 + });
476 + }
452 } 477 }
453 } 478 }
454 </script> 479 </script>
......
1 <!-- 1 <!--
2 * @Date: 2024-10-20 16:57:48 2 * @Date: 2024-10-20 16:57:48
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-10-20 18:28:43 4 + * @LastEditTime: 2024-10-20 18:50:25
5 * @FilePath: /hager/src/views/search.vue 5 * @FilePath: /hager/src/views/search.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -66,6 +66,11 @@ export default { ...@@ -66,6 +66,11 @@ export default {
66 }] 66 }]
67 } 67 }
68 }, 68 },
69 + watch: {
70 + '$route.query.keyword': function(newKeyword) {
71 + this.keyword = newKeyword || '';
72 + }
73 + },
69 mounted () { 74 mounted () {
70 }, 75 },
71 methods: { 76 methods: {
......