0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

前回遷移したページURL取得(リファラ)

Posted at

前ページのURLvue-routerを使ってリファラを取得する

リファラ:直前に滞在していたWebページのこと
import { useRouter } from '#imports'
const router = useRouter()
const previousPath = ref<string | null>("/")

router.beforeEach((to, from, next) => {
  previousPath.value = from.fullPath
  
 // 前回アクセスしたURL
  console.log(previousPath.value)
  
  next()
})

解説

router.beforeEach((to, from, next) => {
// to: 現在から移動しようとするルート
// from: 現在から前のルート
// next(): ナビゲーションを許可する
}

補足

history backでも取得可能みたい

// 1つ前のレコード
router.go(-1)

参考URL:Nuxt.js(Vue.js)でhistory backする方法

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?