Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use beforeRouteEnter in setup hook?

How to use beforeRouteEnter in setup hook?

There is no any mention of onBeforeRouteEnter hook in the documentation. There are only two hooks documented onBeforeRouteLeave and onBeforeRouteUpdate.

like image 818
nitrovatter Avatar asked Mar 04 '21 19:03

nitrovatter


People also ask

How do I use beforerouteenter () in a component?

You can use beforeRouteEnter()in your component as below: beforeRouteEnter (to, from, next) { next(vm => { // access to component's instance using `vm` . // this is done because this navigation guard is called before the component is created. // clear your previously populated search results.

How to access the instance of beforerouteenter in a route?

The beforeRouteEnter guard does NOT have access to this, because the guard is called before the navigation is confirmed, thus the new entering component has not even been created yet. However, you can access the instance by passing a callback to next.

Is it possible to use beforerouteenter with options API?

You can still use beforeRouteEnter in the options API alongside setup if that works for you: Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.

Is there an onbeforerouteenter hook in the documentation?

There is no any mention of onBeforeRouteEnter hook in the documentation. There are only two hooks documented onBeforeRouteLeave and onBeforeRouteUpdate. 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.


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:

like image 154
Dan Avatar answered Oct 16 '22 16:10

Dan