125

Ok, to explain this simply:

I have 3x pages.

  • Page 1 (Home)
  • Page 2 (Menu)
  • Page 3 (About)

Page 1 has a-

<router-link to="/menu">

button that when clicked routes to "/menu".

For now, Page 2 (Menu) has a

<router-link to="/">

button and when this button is clicked it reverts to the previous location "/" Page 1 (Home).

But I don't want to create a component for router for each page to 'go back' to the previous page (as if I had 100 pages this could be a lot of work routing back). Is there a way to do this with vue-router? similar to window.history.back()

I'm curious to see if there is a way to do this as I can't find it in the docs.

starball
  • 20,030
  • 7
  • 43
  • 238
John107
  • 2,157
  • 3
  • 20
  • 36
  • 3
    [router.go(n) | Programmatic Navigation | vue-router](https://router.vuejs.org/en/essentials/navigation.html#routergon) – yuriy636 Jan 04 '18 at 23:08
  • 1
    @yuriy636's is probably the best answer. Currently we have and outer component with the back button which uses string manipulation on the current url to go back to the last page in the route. We will probably change to the router.go method when we get the chance now though – Kartik Prasad Jan 04 '18 at 23:11
  • 1
    Ok thanks! It was staring me right in the face! I'm not really sure how to write this into my syntax though. Sorry! I'm new to this! – John107 Jan 04 '18 at 23:15

8 Answers8

284

You can use Programmatic Navigation. In order to go back, you use this:

router.go(n) 

Where n can be positive or negative (to go back). This is the same as history.back().So you can have your element like this:

<a @click="$router.go(-1)">back</a>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
José Fernando Davila
  • 2,859
  • 1
  • 7
  • 3
  • 1
    Anyone had any issue with this on IE11? E.g if I open a page via router-link, click my button it goes back, however if I open the same page via router-link the `router.go(-1)` clears my URL but nothing happens. – PanPipes Jun 19 '19 at 10:49
  • 1
    I want to go back to previous page if there is no id on load, it works but on page refresh .go(-1) goes 2 pages back rather than 1 page. – Aravindh Gopi Jul 31 '19 at 05:48
  • Noob question, is it ok that we are using `a` tag here instead of router-link – AshotN Sep 13 '19 at 04:16
  • 1
    @Hego555 I believe router-link is a component that renders an `a` tag by default – Steven Soroka Nov 19 '19 at 19:16
  • 1
    This seems to ignore any url params that were in the previous url you are going back to – Titan Mar 25 '20 at 10:46
  • Route-links are usefull to generate links to certain routes because it automatically generated the correct urls when you change history mode on or off. – Roel Mar 19 '21 at 15:28
  • I don't understand why does everybody use @click event on buttons/hyperlinks? It's not accessible, you can't click with your keyboard! What do instead is create a form with a submit handler which will do the logic, then create a submit button inside the form. How do you make an accessible link though? How do you make a programmatic link that shows the route in the browser's bottom left corner (as if the src of the link is set, not an event)? – Seangle Oct 21 '22 at 11:30
  • but its clear params id , i have one page for create and view data . when i am on view data , then go to other page then go back , its on create data not view data . do u have solution ? – Yogi Arif Widodo Jan 19 '23 at 04:30
39

Use $router.back() directly to go back/route-back programmatic on vue-router. Or if in the template of the component use router.back().

maxshuty
  • 9,708
  • 13
  • 64
  • 77
Abhishek Singh
  • 527
  • 7
  • 8
31

This works like a clock for me:

methods: {
 hasHistory () { return window.history.length > 2 }
}

Then, in the template:

<button 
  type="button"    
  @click="hasHistory() 
    ? $router.go(-1) 
    : $router.push('/')" class="my-5 btn btn-outline-success">&laquo; 
  Back
</button>
Derzu
  • 7,011
  • 3
  • 57
  • 60
yeikiu
  • 327
  • 3
  • 4
  • 9
    If you come from another website external to your app, you think that you can go back to a previous page of your app, but you would actually leave it for an external website. – Enric A. Aug 05 '20 at 08:05
13

Worked for me, short and practical. (Nuxt.js)

<a @click="$router.back()"></>
6

If you're using Vuex you can use https://github.com/vuejs/vuex-router-sync

Just initialize it in your main file with:

import VuexRouterSync from 'vuex-router-sync';
VuexRouterSync.sync(store, router);

Each route change will update route state object in Vuex. You can next create getter to use the from Object in route state or just use the state (better is to use getters, but it's other story https://vuex.vuejs.org/en/getters.html), so in short it would be (inside components methods/values):

this.$store.state.route.from.fullPath

You can also just place it in <router-link> component:

<router-link :to="{ path: $store.state.route.from.fullPath }"> 
  Back 
</router-link>

So when you use code above, link to previous path would be dynamically generated.

ojczeo
  • 85
  • 1
  • 3
3

This answer to this question becomes much trickier if you're coding a mobile app as you now need an integrated solution that accommodates back history from either your UI back button OR the "back" button provided by the mobile device!

Frameworks like Ionic and Quasar can intercept mobile device back button events. Quasar handles these events for you and adapts behaviour for desktop and mobile platform builds so a for example a Cordova/Capacitor mobile app build will ensure that a "back" action can intelligently either close a dialog or go back a page or even close the app!

Tony O'Hagan
  • 21,638
  • 3
  • 67
  • 78
  • Quasar v2 no longer provides this directive and link is now broken. It does still intercept back with a different approach. See https://quasar.dev/quasar-plugins/dialog#cordova-capacitor-back-button and https://quasar.dev/quasar-cli/developing-capacitor-apps/configuring-capacitor#quasar-conf-js – xlm Oct 12 '21 at 05:29
0

This might help someone

<router-link class="inner-back d-inline-block" to="">
    <i class="fas fa-arrow-left" @click="$router.go(-1)"></i>
</router-link>
Wasi Sadman
  • 1,382
  • 1
  • 15
  • 27
-5

Another solution is using vue-router-back-mixin

import BackMixin from `vue-router-back-mixin`

export default {
  ...
  mixins: [BackMixin],
  methods() {
    goBack() {
      this.backMixin_handleBack()
    }
  }
  ...
}
ittus
  • 21,730
  • 5
  • 57
  • 57