SPQRInc
SPQRInc

Reputation: 198

onBeforeEnter does not exist in vue-router@next

I am trying to switch over to vuejs3 and the new vue-router.

Now I see that beforeRouteEnter is not exposed:

 // same as beforeRouteUpdate option with no access to `this`
        onBeforeRouteUpdate((to, from, next) => {
            // your logic
            console.log("Hello world") //this is only triggered if the id changes
            next()
        })

So my question is: How can I trigger the initial axios-requests on a specific route like /dashboard as I did before?

Upvotes: 1

Views: 2674

Answers (1)

Dan
Dan

Reputation: 63099

It's not possible to execute code in setup before the route enters because at the time of setup the navigation has already been confirmed.

Another option #1

You can still use the options api, so you could still use beforeRouteEnter:

setup() {
  ...
},
beforeRouteEnter(to, from, next) {
  console.log(to);
}

Another option #2

Use beforeEnter in the router:

{
  path: '/foo',
  component: Foo,
  beforeEnter(to) {
    console.log(to)
  }
}

Upvotes: 2

Related Questions