LoginSignup
3
0

More than 3 years have passed since last update.

【Vue】VueRouterでページ遷移時に、URL履歴を残さない方法

Posted at

はじめに

VueRouterでページ遷移時に、URL履歴が残ってしまうことで少し困ってしまったのでメモを残します。

経緯

先日、Vueを使ってSPAで開発中にページからページCに遷移させる間にローディングページBを挟んで、ローディングページBでいろいろ処理をさせる形で開発をしていました。

その中でページCで、ブラウザバックしたらローディングページBに遷移できてしまうと気付きました。
さらに、ブラウザバック時はクエリパラメータを維持しているURLのままブラウザバックできてしまう...

方法

結論:VueRouterのpushではなくreplaceを使用すること

Example.vue
<template>
  ...
</template>

<script>
  ...
  push () {
    // VueRouterのHistoryに追加して遷移する
    this.$router.push('Next')
  },
  replace () {
    // VueRouterのHistoryに追加しないまま遷移する
    this.$router.replace('Next')
  }
  ...
</script>

参照

3
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
3
0