micro-app环境下,子系统加载vxe-table样式被吞解决方法

卖关子

本地没问题啊,怎么发上去就没样式了呢?

"不应该啊","我本地不这样啊","怎么回事","神奇"。

1.0

先搜索有没有人遇到过类似的问题,找到了这个提问:

在微前端框架micro-app的子应用中使用vxe-table主题样式失效

不过我看了一下,不管怎么样setTheme方法都会调用的,html标签上也出现了属性data-vxe-ui-theme="light"

2.0

在源代码里面搜索,其实是有相关css文件的,但是因为什么原因没生效

Micro-app里面具有样式隔离

虽说基座应用样式可以影响子应用,但是子应用引用的vxe-table下载下来的css样式会出现在子应用的标签里面。

导致的现象是,原本的样式选择器是:

css 复制代码
[data-vxe-ui-theme=light]{
    ...
}

在生效的时候实际上是

css 复制代码
micro-app[name=xxx] [data-vxe-ui-theme=light] {
    ...
}

其中所有选择器都被加上了micro-app[name=xxx]的前缀。

至此,我们知道,想要实现样式选择,需要把data-vxe-ui-theme=light属性加在子应用里面。

3.0

这是源码里面的实现,在html标签上添加了属性documentElement.setAttribute('data-vxe-ui-theme', theme);,那同理可得,我们在micro-app-body标签添加data-vxe-ui-theme即可。

js 复制代码
function setTheme(name) {
  var theme = !name || name === 'default' ? 'light' : name;
  themeConfigStore.theme = theme;
  if (typeof document !== 'undefined') {
    var documentElement = document.documentElement;
    if (documentElement) {
      documentElement.setAttribute('data-vxe-ui-theme', theme);
    }
  }
  return VxeCore;
}

解决方案

在子应用的App.vue中添加下列代码:

js 复制代码
 computed: {
    isMicroAppEnvironment() {
      return window.__MICRO_APP_ENVIRONMENT__;
    },
  },
 methods: {
    setVxeTableTheme() {
      if (!this.isMicroAppEnvironment) return;
      // data-vxe-ui-theme="light" 用于vxe-table样式回显
      const ele = document.querySelector("micro-app-body");
      ele.setAttribute("data-vxe-ui-theme", "light");
    },
  },
相关推荐
vx1_Biye_Design1 分钟前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design2 分钟前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
hedley(●'◡'●)2 分钟前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
qq5_8115175154 分钟前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育5 分钟前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再5 分钟前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架
hdsoft_huge7 分钟前
1panel面板中部署SpringBoot和Vue前后端分离系统 【图文教程】
vue.js·spring boot·后端
这儿有一堆花37 分钟前
Vue 是什么:一套为「真实业务」而生的前端框架
前端·vue.js·前端框架
NCDS程序员1 小时前
v-model: /v-model/ :(v-bind)三者核心区别
前端·javascript·vue.js
小杨同学呀呀呀呀2 小时前
Ant Design Vue <a-timeline>时间轴组件失效解决方案
前端·javascript·vue.js·typescript·anti-design-vue