diff --git a/main.js b/main.js index d700d91..cd0ac36 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,55 @@ import App from './App' import uviewPlus from 'uview-plus' +const TAB_BAR_STYLE_NOT_FOUND_ERROR = 'setTabBarStyle:fail not TabBar page' + +const shouldIgnoreTabBarStyleError = (error) => { + const message = typeof error === 'string' + ? error + : error?.errMsg || error?.message || '' + return message.includes(TAB_BAR_STYLE_NOT_FOUND_ERROR) +} + +const patchSafeSetTabBarStyle = () => { + if (typeof uni === 'undefined' || typeof uni.setTabBarStyle !== 'function') { + return + } + + if (uni.setTabBarStyle.__upSafePatched) { + return + } + + const originalSetTabBarStyle = uni.setTabBarStyle.bind(uni) + const safeSetTabBarStyle = (options = {}) => { + try { + const result = originalSetTabBarStyle(options) + if (result && typeof result.catch === 'function') { + return result.catch((error) => { + if (shouldIgnoreTabBarStyleError(error)) { + return undefined + } + return Promise.reject(error) + }) + } + return result + } catch (error) { + if (shouldIgnoreTabBarStyleError(error)) { + return undefined + } + throw error + } + } + + safeSetTabBarStyle.__upSafePatched = true + uni.setTabBarStyle = safeSetTabBarStyle +} + // #ifndef VUE3 import Vue from 'vue' import './uni.promisify.adaptor' Vue.config.productionTip = false App.mpType = 'app' +patchSafeSetTabBarStyle() const app = new Vue({ ...App }) @@ -15,6 +59,7 @@ app.$mount() // #ifdef VUE3 import { createSSRApp } from 'vue' export function createApp() { + patchSafeSetTabBarStyle() const app = createSSRApp(App) // 使用 uview-plus (版本 3.4.0+) app.use(uviewPlus, () => { @@ -34,4 +79,4 @@ export function createApp() { app } } -// #endif \ No newline at end of file +// #endif