Ver Fonte

优化微前端集成:通过parentWindow参数传递主应用上下文,修复qiankun环境下全局变量访问问题

yusheng há 7 meses atrás
pai
commit
88e2ae54ef
2 ficheiros alterados com 9 adições e 4 exclusões
  1. 3 2
      src/main.js
  2. 6 2
      src/utils/request.js

+ 3 - 2
src/main.js

@@ -60,7 +60,7 @@ Vue.prototype.$handleInputPublic = handleInputPublic;
 
 Vue.component('DictSelection', DictSelection);
 Vue.config.productionTip = false;
-  Vue.component('fileMain', fileMain);
+Vue.component('fileMain', fileMain);
 // 表单重置
 Vue.prototype.resetForm = function resetForm(refName) {
   if (this.$refs[refName]) {
@@ -88,7 +88,8 @@ Vue.use(VueClipboard);
 let instance = null;
 
 function render(props = {}) {
-  const { container, routerBase } = props;
+  const { container, routerBase, parentWindow } = props;
+  window.parentWindow = parentWindow;
 
   // const router = new VueRouter({
   //   base: window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.BASE_URL,

+ 6 - 2
src/utils/request.js

@@ -51,7 +51,9 @@ service.interceptors.response.use(
 
     if (window.__POWERED_BY_QIANKUN__) {
       console.log('主应用的 window 对象:', window.parent);
-      window.parent.window.businessstatus = error;
+      if (window.parentWindow) {
+        window.parentWindow.businessstatus = error;
+      }
     }
 
     // && res.config?.showErrorToast !== false
@@ -80,7 +82,9 @@ service.interceptors.response.use(
   (error) => {
     if (window.__POWERED_BY_QIANKUN__) {
       console.log('主应用的 window 对象:', window.parent);
-      window.parent.window.businessstatus = error;
+      if (window.parentWindow) {
+        window.parentWindow.businessstatus = error;
+      }
     }
     // 登录过期处理
     if (error?.response?.status === 401) {