How to use beforeRouteEnter in setup hook?

Viewed 9040
1 Answers

In the composition API, the timing of setup roughly equates to created in Vue 2. By that time, the routing has already occurred, so there would be no meaning to a beforeRouteEnter inside of setup.

You can still use beforeRouteEnter in the options API alongside setup if that works for you:

setup() {
  console.log('SETUP')
},
beforeRouteEnter(to, from, next) {
  // Do something
  next({ path: '/foo' }); // Go somewhere else if necessary
  next();                 // Or stay here
}

Or beforeEnter or beforeEach in the router might serve your purposes:

Related