From 71ba22bedc3b7dacb2d579aa87986d1d7606ea0a Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Mon, 28 Sep 2026 17:25:10 +0800 Subject: [PATCH] =?UTF-8?q?h5=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sheep/stores/tabbar.ts | 52 +++++++++++++++++++++++++------------- vite.config.ts | 3 ++- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/sheep/stores/tabbar.ts b/src/sheep/stores/tabbar.ts index 917b4d7..714ff4b 100644 --- a/src/sheep/stores/tabbar.ts +++ b/src/sheep/stores/tabbar.ts @@ -152,15 +152,20 @@ export const useTabbarStore = defineStore("tabbar", { if (!this._getNativeTabbarPath()) { return } + if (typeof uni.setTabBarItem !== "function") return this.allTabbarList.forEach((item, index) => { const visible = this.visibleTabbarList.some((current) => current.pagePath === item.pagePath) - uni.setTabBarItem({ - index, - text: this.resolveTabText(item), - iconPath: item.iconPath, - selectedIconPath: item.selectedIconPath, - visible, - }) + try { + uni.setTabBarItem({ + index, + text: this.resolveTabText(item), + iconPath: item.iconPath, + selectedIconPath: item.selectedIconPath, + visible, + }) + } catch (err) { + console.log("setTabBarItem error:", err) + } }) }, reset() { @@ -284,24 +289,31 @@ export const useTabbarStore = defineStore("tabbar", { if (!targetPath || this.isTabbarPage(targetPath)) return this.hideTabbar(currentPath) }, - // 原生 tabbar 相关 API 只能在 tabbar 页面执行,这里统一做保护。 + // 原生 tabbar 相关 API 只能在 tabbar 页面执行;H5 部分环境未实现,需先探测再调用,避免打断路由。 _applyNativeTabbarVisibility(path = "") { const nativeTabbarPath = this._getNativeTabbarPath(path) if (!nativeTabbarPath) return const action = this.tabbarVisible ? "showTabBar" : "hideTabBar" - uni[action]({ - animation: true, - success: (res) => { - console.log(`${action}:`, res) - }, - fail: (err) => { - console.log(`${action} fail:`, err) - }, - }) + const api = (uni as any)[action] + if (typeof api !== "function") return + try { + api({ + animation: true, + success: (res: any) => { + console.log(`${action}:`, res) + }, + fail: (err: any) => { + console.log(`${action} fail:`, err) + }, + }) + } catch (err) { + console.log(`${action} error:`, err) + } }, // 原生 tabbar 不能动态增删项,只能按索引逐项控制可见性。 _applyNativeTabbarItems() { if (!this._getNativeTabbarPath()) return + if (typeof uni.setTabBarItem !== "function") return this.allTabbarList.forEach((item, index) => { const visible = this.visibleTabbarList.some((current) => current.pagePath === item.pagePath) const options: any = { @@ -311,7 +323,11 @@ export const useTabbarStore = defineStore("tabbar", { selectedIconPath: item.selectedIconPath, visible, } - uni.setTabBarItem(options) + try { + uni.setTabBarItem(options) + } catch (err) { + console.log("setTabBarItem error:", err) + } }) }, // 统一监听全局路由跳转,避免每个页面都手动调用 show/hideTabBar。 diff --git a/vite.config.ts b/vite.config.ts index b167232..70c4d6a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -29,7 +29,8 @@ export default defineConfig(({ mode }) => { throw new Error("生产环境必须使用 HTTPS 协议!请检查 VITE_APP_PROD_SERVICE 配置") } return { - base: "/h5/", + // H5 部署子路径只在 manifest.json → h5.router.base 配置; + // 勿在此处写死 /h5/,否则 App(iOS/Android) 打包也会带上错误前缀。 build: { target: "es6", sourcemap: false,