fix(课程列表页): 修复路由参数变化时搜索未触发的问题
移除对相同关键字的拦截逻辑,确保路由参数变化时能触发搜索。改用精确监听路由参数并设置立即执行,解决进入页面时搜索未触发的问题。
Showing
2 changed files
with
29 additions
and
11 deletions
| ... | @@ -58,3 +58,8 @@ https://oa-dev.onwall.cn/f/mlaj | ... | @@ -58,3 +58,8 @@ https://oa-dev.onwall.cn/f/mlaj |
| 58 | - 课程页(`isCoursePage=true`):回车或搜索键触发后跳转至 `/courses-list` 并携带 `keyword` 参数。 | 58 | - 课程页(`isCoursePage=true`):回车或搜索键触发后跳转至 `/courses-list` 并携带 `keyword` 参数。 |
| 59 | - 列表页:仅更新当前路由的查询参数 `keyword`。 | 59 | - 列表页:仅更新当前路由的查询参数 `keyword`。 |
| 60 | - 位置:`/src/components/ui/SearchBar.vue`,新增 `useFormSubmit` 可选参数(默认开启)。 | 60 | - 位置:`/src/components/ui/SearchBar.vue`,新增 `useFormSubmit` 可选参数(默认开启)。 |
| 61 | + - 课程列表页搜索触发修复 | ||
| 62 | + - 现象:进入列表页后,输入关键字并更新为 `/courses-list?keyword=xxx` 时未触发搜索。 | ||
| 63 | + - 原因:父组件 `handleSearch` 对“相同关键字”进行了拦截,导致路由参数变化不执行请求。 | ||
| 64 | + - 修复:移除拦截逻辑;无论关键字是否变化均触发搜索(防抖控制频率)。 | ||
| 65 | + - 位置:`/src/views/courses/CourseListPage.vue` 的 `handleSearch`。 | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-21 14:31:21 | 2 | * @Date: 2025-03-21 14:31:21 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-05-21 16:57:36 | 4 | + * @LastEditTime: 2025-12-08 11:31:57 |
| 5 | * @FilePath: /mlaj/src/views/courses/CourseListPage.vue | 5 | * @FilePath: /mlaj/src/views/courses/CourseListPage.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -30,7 +30,7 @@ | ... | @@ -30,7 +30,7 @@ |
| 30 | </template> | 30 | </template> |
| 31 | 31 | ||
| 32 | <script setup> | 32 | <script setup> |
| 33 | -import { ref, onMounted, watchEffect } from 'vue'; | 33 | +import { ref, onMounted, watch } from 'vue'; |
| 34 | import { useRoute } from 'vue-router'; | 34 | import { useRoute } from 'vue-router'; |
| 35 | import AppLayout from '@/components/layout/AppLayout.vue'; | 35 | import AppLayout from '@/components/layout/AppLayout.vue'; |
| 36 | import SearchBar from '@/components/ui/SearchBar.vue'; | 36 | import SearchBar from '@/components/ui/SearchBar.vue'; |
| ... | @@ -60,20 +60,33 @@ const debouncedSearch = useDebounceFn(async (searchKeyword) => { | ... | @@ -60,20 +60,33 @@ const debouncedSearch = useDebounceFn(async (searchKeyword) => { |
| 60 | }, 300); | 60 | }, 300); |
| 61 | 61 | ||
| 62 | // 统一处理搜索操作 | 62 | // 统一处理搜索操作 |
| 63 | +/** | ||
| 64 | + * @function handleSearch | ||
| 65 | + * @description 统一触发搜索;即使关键字相同也重新搜索,确保从URL跳转和失焦行为都能触发。 | ||
| 66 | + * @param {string} newKeyword 新关键字 | ||
| 67 | + * @returns {void} | ||
| 68 | + */ | ||
| 63 | const handleSearch = (newKeyword) => { | 69 | const handleSearch = (newKeyword) => { |
| 64 | - if (keyword.value !== newKeyword) { | ||
| 65 | keyword.value = newKeyword; | 70 | keyword.value = newKeyword; |
| 66 | debouncedSearch(newKeyword); | 71 | debouncedSearch(newKeyword); |
| 67 | - } | ||
| 68 | }; | 72 | }; |
| 69 | 73 | ||
| 70 | -// 监听路由参数变化 | 74 | +// 精确监听路由查询参数 keyword 的变化 |
| 71 | -watchEffect(() => { | 75 | +/** |
| 72 | - const queryKeyword = $route.query.keyword; | 76 | + * @function watch_route_keyword |
| 73 | - if (queryKeyword !== undefined) { | 77 | + * @description 精确监听 `$route.query.keyword` 的变化并触发搜索; |
| 74 | - handleSearch(queryKeyword || ''); | 78 | + * `immediate: true` 保证进入页面时若已有关键字能立刻执行搜索。 |
| 75 | - } | 79 | + * @returns {void} |
| 76 | -}); | 80 | + */ |
| 81 | +watch( | ||
| 82 | + () => $route.query.keyword, | ||
| 83 | + (queryKeyword) => { | ||
| 84 | + if (queryKeyword !== undefined) { | ||
| 85 | + handleSearch(queryKeyword || ''); | ||
| 86 | + } | ||
| 87 | + }, | ||
| 88 | + { immediate: true } | ||
| 89 | +); | ||
| 77 | 90 | ||
| 78 | // Blur handler | 91 | // Blur handler |
| 79 | const handleBlur = (query) => { | 92 | const handleBlur = (query) => { | ... | ... |
-
Please register or login to post a comment