4

I am using $router.go(-1) to go back to the previous page but as I am using this on a home page I want know how to check if there is a previous route.

xlm
  • 5,564
  • 13
  • 48
  • 52
selam
  • 43
  • 1
  • 5
  • Related: https://stackoverflow.com/questions/3588315/how-to-check-if-the-user-can-go-back-in-browser-history-or-not – xlm Oct 12 '21 at 05:17

1 Answers1

7

Vue router navigation is tracked by the native browser history, use window.history.length.

If it is 0 there is nothing to go back to.

However if the user has come from another website already is a value higher than 0 upon your App mounting you could store this initial value of window.history.length globally on beforeMount or mounted in say this.$root.historyCount (or if you are using vuex already use that), then when you check window.history.length again on a call to action, subtract $root.historyCount from window.history.length to see if zeroes out.

This way you will be only counting history in your own app.

You may also want to consider that perhaps if your current route ($router.currentRoute) is the home page there is also no way of going back beyond that page.

Marc
  • 4,197
  • 1
  • 29
  • 37