From a26208dd20c915bc12eb075e29a61f883e7b7a8b Mon Sep 17 00:00:00 2001 From: wslyx Date: Sat, 10 May 2025 14:37:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E8=B7=B3=E8=BD=AC=E6=97=B6=E7=9A=84=20debounce=20?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 go 函数中添加逻辑,当跳转到新路径时清除遗留的 debounce 状态 - 通过调用 debouncedGoFunc.cancel() 方法来取消上一次的延迟执行 - 这个修复确保了路由跳转的正确性和及时性,避免了潜在的性能问题 --- sheep/router/index.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/sheep/router/index.js b/sheep/router/index.js index 0a4ecc30..64ad4ac5 100644 --- a/sheep/router/index.js +++ b/sheep/router/index.js @@ -1,7 +1,7 @@ import $store from '@/sheep/store'; import { showAuthModal, showShareModal } from '@/sheep/hooks/useModal'; import { isNumber, isString, isEmpty, startsWith, isObject, isNil, clone } from 'lodash-es'; -import throttle from '@/sheep/helper/throttle'; +import { debounce } from 'lodash'; const _go = ( path, @@ -68,7 +68,7 @@ const _go = ( url += `?${query}`; } - // 跳转底部导航 + // 跳转底部导航,TABBAR由Vite配置插件注入的,在uniReadPagesV3Plugin中 if (TABBAR.includes(page)) { uni.switchTab({ url, @@ -89,11 +89,27 @@ const _go = ( }); }; -// 限流 防止重复点击跳转 +// 防抖 防止重复点击跳转 +let go_args; +const debouncedGoFunc = debounce( + () => { + _go(...go_args); + }, + 500, + { + //在延迟开始前立即调用事件 + leading: true, + //在延时结束后不调用,保证事件只被触发一次 + trailing: false, + } +); function go(...args) { - throttle(() => { - _go(...args); - }); + // 向新的路径跳转时清除遗留debounce的状态 + if (args !== go_args) { + debouncedGoFunc.cancel(); + } + go_args = args; + debouncedGoFunc(); } function paramsToQuery(params) {