LoginSignup
1
0

More than 3 years have passed since last update.

Nuxt.jsでの「localstorage is not defined」への対応

Posted at

Nuxt.jsでの開発でdata内でlocalstorageを使おうとしたときに「localstorage is not defined」エラーが出た時の対応メモです。

以下のようにdata内で直接呼び出すとサーバサイドレンダリングしているのでブラウザのストレージにアクセスできずエラーになる。

data () {
  isLoggin: localStorage.currentUser.token !== null
}

対応

以下のようにmounted内でアクセスすれば動作する

  data () {
    isLoggin: false
  },
  mounted () {
    if (localStorage.currentUser) {
      this.isLoggin = localStorage.currentUser.token !== null
    }
  }

参考
https://jp.vuejs.org/v2/cookbook/client-side-storage.html

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0