fix: 修复 Layout.vue SSR 构建警告 (document is not defined)

This commit is contained in:
MengStack Dev 2026-10-03 00:57:00 +08:00
parent b717fdb076
commit 34ef95cdf4

View File

@ -1,18 +1,26 @@
<script setup>
import DefaultTheme from 'vitepress/theme'
import { useRoute } from 'vitepress'
import { computed, watch, onUnmounted } from 'vue'
import { computed, watch, onMounted, onUnmounted } from 'vue'
import Home from './Home.vue'
const route = useRoute()
const isHome = computed(() => route.path === '/' || route.path === '/index.html')
watch(isHome, (val) => {
function toggleHomeLayout(val) {
if (typeof document !== 'undefined') {
document.documentElement.classList.toggle('home-layout', val)
}, { immediate: true })
}
}
watch(isHome, toggleHomeLayout, { immediate: true })
onMounted(() => toggleHomeLayout(isHome.value))
onUnmounted(() => {
if (typeof document !== 'undefined') {
document.documentElement.classList.remove('home-layout')
}
})
</script>