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

修正Nuxt中導(dǎo)航器/視窗/文件未定義的方法
P粉143640496
P粉143640496 2024-03-25 19:00:40
0
1
594

我試圖確定 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

全部回覆(1)
P粉242126786

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

  • navigator 未定義
  • window 未定義
  • #文件未定義

#這是一個(gè)關(guān)於如何包裝邏輯 JS 程式碼的範(fàn)例

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)站素材
前端模板