From 8e76de0313ebf29bf95459ece9118026e1eff873 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Thu, 21 Aug 2025 20:10:47 +0800 Subject: [PATCH] =?UTF-8?q?chore(router):=20=E4=BF=AE=E5=A4=8DswitchTab?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=90=BA=E5=B8=A6query=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复uni-app中switchTab跳转时query参数丢失的问题,改为直接使用完整path跳转 优化路由拦截器中的query参数合并逻辑,确保参数正确传递 --- src/pages/login/login.vue | 12 +++++++++--- src/router/interceptor.ts | 26 ++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue index b09f8b9..b165c91 100644 --- a/src/pages/login/login.vue +++ b/src/pages/login/login.vue @@ -39,15 +39,21 @@ function doLogin() { path = `/${path}` } const { path: _path, query } = parseRouteStr(path) - console.log('_path:', _path, 'query:', query) + console.log('_path:', _path, 'query:', query, 'path:', path) console.log('isPageTabbar(_path):', isPageTabbar(_path)) if (isPageTabbar(_path)) { + // 经过我的测试 switchTab 不能带 query 参数, 不管是放到 url 还是放到 query , + // 最后跳转过去的时候都会丢失 query 信息 uni.switchTab({ - url: _path, - query, + url: path, }) + // uni.switchTab({ + // url: _path, + // query, + // }) } else { + console.log('redirectTo:', path) uni.redirectTo({ url: path, }) diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts index 5351054..cfd7110 100644 --- a/src/router/interceptor.ts +++ b/src/router/interceptor.ts @@ -18,38 +18,32 @@ export const navigateToInterceptor = { if (url === undefined) { return } + let { path, query: _query } = parseRouteStr(url) - let path = decodeURIComponent(url).split('?')[0] + console.log('路由拦截器 1: url->', url, ', query ->', query) + const myQuery = { ..._query, ...query } // /pages/route-interceptor/index?name=feige&age=30 - console.log('路由拦截器:url->', url, ', query ->', query, ', path ->', path) + console.log('路由拦截器 2: path->', path, ', _query ->', _query) + console.log('路由拦截器 3: myQuery ->', myQuery) // 处理相对路径 if (!path.startsWith('/')) { - console.log(getCurrentPages()) - if (getCurrentPages().length === 0) { - return - } const currentPath = getLastPage()?.route || '' const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}` const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/')) path = `${baseDir}/${path}` } - const { path: _path, query: _query } = parseRouteStr(path) - console.log('_path:', _path, 'query:', _query) - // 处理直接进入路由非首页时,tabbarIndex 不正确的问题 - tabbarStore.setAutoCurIdx(_path) + tabbarStore.setAutoCurIdx(path) - if (LOGIN_PAGE_LIST.includes(_path)) { + if (LOGIN_PAGE_LIST.includes(path)) { console.log('命中了 LOGIN_PAGE_LIST') return } - console.log('拦截器中得到的 path:', path) - console.log('拦截器中得到的 query:', query) - if (query) { - path += `?${Object.keys(query).map(key => `${key}=${query[key]}`).join('&')}` + if (myQuery) { + path += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}` } const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(path)}` @@ -62,7 +56,7 @@ export const navigateToInterceptor = { return } else { - if (EXCLUDE_PAGE_LIST.includes(_path)) { + if (EXCLUDE_PAGE_LIST.includes(path)) { return } else {