Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hook beforeRouteEnter in NuxtJs

Tags:

vue.js

nuxt.js

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?

like image 599
chawila Avatar asked Jul 11 '19 09:07

chawila


1 Answers

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 /
like image 181
Wonderman Avatar answered Oct 12 '22 19:10

Wonderman