解决浏览器端 globalThis is not defined 报错
解决浏览器端 globalThis is not defined 报错
场景:
最近在使用 nuxt.js 做服务端渲染的移动端应用时,我们在app里引用h5的页面。结果上线后在部分机型(例如:Mozilla/5.0 (Linux; Android 8.1.0; M1822 Build/OPM1.171019.026; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.110 Mobile Safari/537.36)里页面基本结构能加载出来但是页面不会发起其他网络请求。用 vConsole 调试也看不到报错,最后安卓开发同事调试才发现报错。如下图:
解决办法:
在html头部加上如下代码:
<head>
<script>
this.globalThis || (this.globalThis = this)
</script>
...
</head>
复制代码
思路是:既然全局上没有定义 globalThis 那我们就判断一下,如果没有的情况下,定义全局的一个 globalThis 指向全局 this 对象(也就是 window 对象)。(注:支持 globalThis 的浏览器,其指向就是 window 对象,更多信息可在这里查看:developer.mozilla.org/en-US/docs/…
关键词:globalThis is not defined, nuxt.js, globalThis
评论
热门评论
多厘
开发 @ 小有记4月前来来来, 收个膝盖..![[害羞]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_7.eca1535.png)
![[害羞]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_7.eca1535.png)
点赞
1
its_wild
(作者)4月前![[偷笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_20.7a46372.png)
点赞
回复
全部评论 6
最新
最热
欲可非
前端开发3月前感谢分享,解决了我的企微和微信浏览器问题
1
回复
鸭鸭不在乎T_T
3月前解决了我的问题
,新项目用Vue3和Element Plus,也是这个报错
![[赞]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_108.a6defc6.png)
1
回复
多厘
开发 @ 小有记4月前来来来, 收个膝盖..![[害羞]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_7.eca1535.png)
![[害羞]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_7.eca1535.png)
点赞
1
its_wild
(作者)4月前![[偷笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_20.7a46372.png)
点赞
回复
Tims18294
前端开发5月前在一个 chrome 70版本上复现了次问题,用上述方法解决了。感谢了![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
![[憨笑]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_27.0006230.png)
点赞
1
its_wild
(作者)4月前不客气![[愉快]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_21.c408154.png)
![[愉快]](https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/jj_emoji_21.c408154.png)
漫思