fix: 修复 Layout.vue SSR 构建警告 (document is not defined)
This commit is contained in:
parent
b717fdb076
commit
34ef95cdf4
@ -1,18 +1,26 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import DefaultTheme from 'vitepress/theme'
|
import DefaultTheme from 'vitepress/theme'
|
||||||
import { useRoute } from 'vitepress'
|
import { useRoute } from 'vitepress'
|
||||||
import { computed, watch, onUnmounted } from 'vue'
|
import { computed, watch, onMounted, onUnmounted } from 'vue'
|
||||||
import Home from './Home.vue'
|
import Home from './Home.vue'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const isHome = computed(() => route.path === '/' || route.path === '/index.html')
|
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)
|
document.documentElement.classList.toggle('home-layout', val)
|
||||||
}, { immediate: true })
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(isHome, toggleHomeLayout, { immediate: true })
|
||||||
|
|
||||||
|
onMounted(() => toggleHomeLayout(isHome.value))
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
|
if (typeof document !== 'undefined') {
|
||||||
document.documentElement.classList.remove('home-layout')
|
document.documentElement.classList.remove('home-layout')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user