如何修复导航器 /窗口 /文档在NUXT中不确定
我试图确定NUXT应用程序中的用户和视网膜信息。但是该应用程序正在抛出错误,显示Navigatior /窗口不确定。如何在NUXT应用程序中获取这些信息?
const userAgent = navigator.userAgent.toLowerCase()
const isAndroid = userAgent.includes('android')
isRetina() {
let mediaQuery
if (typeof window !== 'undefined' && window !== null) {
mediaQuery =
'(-webkit-min-device-pixel-ratio: 1.25), (min--moz-device-pixel-ratio: 1.25), (-o-min-device-pixel-ratio: 5/4), (min-resolution: 1.25dppx)'
if (window.devicePixelRatio > 1.25) {
return true
}
if (window.matchMedia && window.matchMedia(mediaQuery).matches) {
return true
}
}
return false
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是修复的解决方案:
导航器是未定义的
窗口是未定义的
文档未定义
这是有关如何包装逻辑JS的示例 https://nuxtjs.s.org/docs/2.x/
如下所示: internals-glossary/context
ps:
已安装
+process.client
有点冗余,因为已安装
仅在客户端。另外,将组件包装到
< climly-gt;
如果您只想使用它的客户端也是一个好主意。此文件的文档在这里:
ps:当心,因为此
client> client> client-inly
tag只能跳过渲染,而不是执行,为在此处解释。当您 import 它们时,有些软件包不支持
vue-editor
)This is the solution to fix:
navigator is undefined
window is undefined
document is not defined
Here is an example on how you should wrap your logic JS code
As shown here: https://nuxtjs.org/docs/2.x/internals-glossary/context
PS:
mounted
+process.client
are kinda redundant becausemounted
only runs on the client.Also, wrapping your component into
<client-only>
if you want it to be only client side rendered is also a good idea.The documentation for this one is here: https://nuxtjs.org/docs/2.x/features/nuxt-components/#the-client-only-component
PS: beware because this
client-only
tag will only skip the rendering, not the execution, as explained here.Some packages do not support SSR when you import them, for that you could either:
vue-editor
)