fix: 解决window.xxx在路由报错情况下的undefined问题

This commit is contained in:
dap
2026-03-23 11:38:51 +08:00
parent 46e8d50d40
commit 3a2d051992
3 changed files with 43 additions and 13 deletions

View File

@@ -1,25 +1,27 @@
<script lang="ts" setup>
import type { ConfigProviderProps } from "antdv-next";
import type { ConfigProviderProps } from 'antdv-next';
import { computed } from "vue";
import { computed } from 'vue';
import { useAntdDesignTokens } from "@vben/hooks";
import { preferences, usePreferences } from "@vben/preferences";
import { useAntdDesignTokens } from '@vben/hooks';
import { preferences, usePreferences } from '@vben/preferences';
import { App, ConfigProvider, theme } from "antdv-next";
import { App, ConfigProvider, theme } from 'antdv-next';
import { antdLocale } from "#/locales";
import { antdLocale } from '#/locales';
import { waveConfigs } from "./components/global/button-wave";
import { PopupContext } from "./utils/context";
import { waveConfigs } from './components/global/button-wave';
import { PopupContext } from './utils/context';
defineOptions({ name: "App" });
defineOptions({ name: 'App' });
const { isDark } = usePreferences();
const { tokens } = useAntdDesignTokens();
const tokenTheme = computed(() => {
const algorithm = isDark.value ? [theme.darkAlgorithm] : [theme.defaultAlgorithm];
const algorithm = isDark.value
? [theme.darkAlgorithm]
: [theme.defaultAlgorithm];
// antd 紧凑模式算法
if (preferences.app.compact) {
@@ -39,7 +41,9 @@ const waveConfig = computed(() => {
return found ? found.wave : {};
});
const otherProps = computed<Omit<ConfigProviderProps, "locale" | "theme" | "wave">>(() => {
const otherProps = computed<
Omit<ConfigProviderProps, 'locale' | 'theme' | 'wave'>
>(() => {
// 目前不生效?
return {
modal: { mask: { blur: false } },
@@ -49,7 +53,12 @@ const otherProps = computed<Omit<ConfigProviderProps, "locale" | "theme" | "wave
</script>
<template>
<ConfigProvider :locale="antdLocale" :theme="tokenTheme" :wave="waveConfig" v-bind="otherProps">
<ConfigProvider
:locale="antdLocale"
:theme="tokenTheme"
:wave="waveConfig"
v-bind="otherProps"
>
<App :message="{ maxCount: 1 }">
<RouterView />
<PopupContext />

View File

@@ -17,6 +17,7 @@ import { initComponentAdapter } from './adapter/component';
import { initSetupVbenForm } from './adapter/form';
import App from './app.vue';
import { router } from './router';
import { initPopupContext } from './utils/context';
async function bootstrap(namespace: string) {
// 初始化组件适配器
@@ -58,6 +59,9 @@ async function bootstrap(namespace: string) {
const { initTippy } = await import('@vben/common-ui/es/tippy');
initTippy(app);
// 初始化全局弹窗方法(静态API fallback, 防止路由守卫中访问undefined)
initPopupContext();
// 配置路由及路由守卫
app.use(router);

View File

@@ -1,6 +1,23 @@
import { defineComponent } from 'vue';
import { App } from 'antdv-next';
import { App, message, Modal, notification } from 'antdv-next';
/**
* 初始化全局弹窗方法(静态API作为fallback)
* 在app.mount之前调用, 防止路由守卫等早期逻辑访问window.message时报错
* app.mount之后PopupContext会用带上下文的实例覆盖
*/
export function initPopupContext() {
if (!window.message) {
window.message = message;
}
if (!window.modal) {
window.modal = Modal;
}
if (!window.notification) {
window.notification = notification;
}
}
/**
* 弹窗上下文