how to hook beforeRouteEnter in NuxtJs

11,991

Trying your code in my current project based on nuxt ˆ2.0.0, I report to you my dev tools console, after navigation from / route to /_slug route.

_slug.js:200 before route called
_slug.js:192 created
_slug.js:198 mounted
_slug.js:205 prev rout is: undefined

When I refresh the page _slug is the same.

If you want the previous route why don't simply do:

beforeRouteEnter(to, from, next) {
   console.log("before route called");
   const previousRoute = from.path || from.fullPath
   console.log(`Previous Route ${previousRoute}`)
}

Which log is:

_slug.js:200 before route called
_slug.js:202 Previous Route /
Share:
11,991
chawila
Author by

chawila

Updated on June 04, 2022

Comments

  • chawila
    chawila almost 2 years

    I am trying to get the previous route for my nuxtjs app, so for i used beforeRouteEnter, but beforeRouteEnter is not firing.

    i tried to add key in nuxt-link : and still not working..

    <nuxt-link :key="$route.path" :to="'/services/'+service.id">
    

    so here is my code:

    
      created: function(){
        console.log("created");
      },
    
      mounted: function(){
        console.log("mounted");
      },
    
      mounted: function(){
        console.log("mounted");
      },
    
      beforeRouteEnter(to, from, next) {
    
        console.log("before route called");
    
        next(vm => {
          console.log("prev rout is: "+vm.prevRoute);
        })
    
      }
    

    i expected :

    before route called
    prev rout is: /services
    created
    mounted
    

    but i only get :

    created
    mounted
    

    Is there anything that i'm doing wrong?