亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

修復(fù)Nuxt中導(dǎo)航器/窗口/文檔未定義的方法
P粉143640496
P粉143640496 2024-03-25 19:00:40
0
1
595

我試圖確定 Nuxt 應(yīng)用程序內(nèi)的 UserAgent 和 Retina 信息。但應(yīng)用程序拋出錯(cuò)誤并顯示導(dǎo)航/窗口未定義。我如何在 nuxt 應(yīng)用程序中獲取這些信息?

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
}

P粉143640496
P粉143640496

全部回復(fù)(1)
P粉242126786

這是要修復(fù)的解決方案:

  • navigator 未定義
  • window 未定義
  • 文檔未定義

這是一個(gè)關(guān)于如何包裝邏輯 JS 代碼的示例

sssccc

如下所示:https://nuxtjs.org/docs/2。 x/internals-glossary/context

PS: mounted + process.client 有點(diǎn)多余,因?yàn)?mounted 僅運(yùn)行在 客戶端。


此外,如果您希望組件僅在客戶端呈現(xiàn),則將組件包裝到 <client-only> 也是一個(gè)好主意。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板