Nuxt4页面刷新报错:Cannot read properties of undefined (reading 'innerWidth')
Vue
2025-11-15 10:46:28
nuxt刷新页面,项目报错,错误信息为:Cannot read properties of undefined (reading 'innerWidth')
原因分析:这个错误通常是因为在服务端渲染 (SSR) 时访问了浏览器特有的 API(如 window、document 等)
解决错误:
使用 onMounted 确保在客户端执行
<script setup lang="ts">
import { ref, onMounted } from 'vue'
const windowWidth = ref(0)
onMounted(() => {
// 只在客户端执行
windowWidth.value = window.innerWidth
})
</script>错误预览:

六月初字帖坊小程序
你想要的字帖模板及工具,这里都有!
904篇文章
17人已阅读
六月初