8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Vue]一度だけしか実行しないbeforeEachを登録する[Nuxt]

Last updated at Posted at 2021-07-19

VueRouterでページ遷移に処理をフックする際に利用するbeforeEachで一回だけしか実行しない処理を登録するにはどうしたら良いでしょう?
答えは簡単で、beforeEachで関数を登録すると、その返り値はその関数を解除するための関数が返されます。

const unregister = router.beforeEach(() => {
  console.log('一回だけ・・・')
  unregister()
})

これで自分自身を登録しつつ、実行された場合に登録解除されて次回以降の遷移では実行されません。
おしまい

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?