|
@@ -364,31 +364,34 @@ onUnmounted(() => {
|
|
|
})
|
|
|
|
|
|
|
|
|
-// 动态加载样式
|
|
|
+
|
|
|
const loadStyles = async () => {
|
|
|
try {
|
|
|
- // 确保先移除旧样式
|
|
|
- if (currentStyle) {
|
|
|
- document.head.removeChild(currentStyle)
|
|
|
- currentStyle = null
|
|
|
- }
|
|
|
+ // 1. 首先移除所有已存在的样式
|
|
|
+ const existingStyles = document.head.querySelectorAll('style[data-theme]')
|
|
|
+ existingStyles.forEach(style => {
|
|
|
+ document.head.removeChild(style)
|
|
|
+ })
|
|
|
|
|
|
+ // 2. 创建新的样式元素
|
|
|
const styleElement = document.createElement('style')
|
|
|
styleElement.type = 'text/css'
|
|
|
+ // 添加标识,方便后续清理
|
|
|
+ styleElement.setAttribute('data-theme', isYunNanProject.value ? 'yunnan' : 'default')
|
|
|
|
|
|
- // 使用动态import加载样式模块
|
|
|
+ // 3. 加载对应的样式模块
|
|
|
const module = isYunNanProject.value
|
|
|
- ? await import('./test-yn/index-yn.scss?inline') // 添加?inline确保直接获取样式内容
|
|
|
+ ? await import('./test-yn/index-yn.scss?inline')
|
|
|
: await import('./index.scss?inline')
|
|
|
|
|
|
styleElement.textContent = module.default
|
|
|
document.head.appendChild(styleElement)
|
|
|
currentStyle = styleElement
|
|
|
|
|
|
- // 强制触发重绘
|
|
|
- document.body.style.display = 'none'
|
|
|
- document.body.offsetHeight // 触发重排
|
|
|
- document.body.style.display = ''
|
|
|
+ // 4. 强制重新计算样式
|
|
|
+ document.documentElement.style.display = 'none'
|
|
|
+ document.documentElement.offsetHeight // 触发重排
|
|
|
+ document.documentElement.style.display = ''
|
|
|
|
|
|
return styleElement
|
|
|
} catch (error) {
|
|
@@ -396,18 +399,14 @@ const loadStyles = async () => {
|
|
|
return null
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
let currentStyle = null
|
|
|
|
|
|
|
|
|
|
|
|
// 监听项目类型变化,动态切换样式
|
|
|
watch(() => isYunNanProject.value, async (newVal) => {
|
|
|
- if (currentStyle) {
|
|
|
- document.head.removeChild(currentStyle)
|
|
|
- currentStyle = null
|
|
|
- }
|
|
|
-
|
|
|
- currentStyle = await loadStyles()
|
|
|
+ await loadStyles()
|
|
|
console.log('样式已更新:', newVal ? 'yunnan' : 'default')
|
|
|
}, { immediate: true })
|
|
|
// 添加watch来更新isYunNanProject
|