LoginSignup
1
1

More than 1 year has passed since last update.

nuxt.jsで特定のページでのリロード対策

Posted at

コード

<script>
export default {
  destroyed () {
    window.removeEventListener('beforeunload', this.confirmReload)
    window.removeEventListener('unload', this.confirmAfterReload)
  },
  mounted () {
    window.addEventListener('beforeunload', this.confirmReload)
    window.addEventListener('unload', this.confirmAfterReload)
  },
  created() {
    if(localStorage.getItem('reload-flg')) {
      localStorage.removeItem('reload-flg')
      this.$router.push('/dummyUrl')  ここに遷移させたいURLを記載
      return
    }
  },
  methods: {
    confirmReload (event) {
      event.returnValue = 'このサイトを再読み込みしますか?'
    },
    confirmAfterReload () {
      localStorage.setItem("reload-flg", true)
    }
  }
}
</script>

localstrageを使用して力技で、画面遷移させました。。!
(ちなみにモードはSPAです)

vuexで状態管理していたらリロードされた場合に、
画面遷移させたいケースって結構ある気がしますが、
みなさんどうしているのだろうか??

1
1
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
1