Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt - How to find the previous route?

I am able to use this.$router.go(-1) to redirect a user to the previous route. However, I am not able to understand how I get get the information about the previous route before redirecting.

Basically, by first reading what the previous route was, I want to make sure that the previous route was from the same domain and only then redirect to it, otherwise do something else.

like image 415
asanas Avatar asked Mar 18 '19 11:03

asanas


2 Answers

In your page you can add asyncData hook which have access to context object with from property:

asyncData({ from }) {
  console.log(from);
}
like image 97
David Go Avatar answered Oct 04 '22 15:10

David Go


in nuxt static methods where you have access to nuxt context ex: asyncData or middlewares :

asyncData({from}){
    // do something with from
}

but if you want to get the prev route in vue instance you can use

this.$nuxt.context.from

also, remember that from can be undefined if there wasn't any prev page in the browser history

like image 28
Zoha Avatar answered Oct 04 '22 15:10

Zoha