Vue-router: How can I go back ?

Created on 16 Jul 2017  路  5Comments  路  Source: vuejs/vue-router

What problem does this feature solve?

Like #575 - I have 3 path

1. _/_ (name: root)
2. _/first_ (name: first)
3. _/first/second_ (name: second)

I do $route.push({name: first}); And next $route.push({name: second});

How can I go back? (click "close" for modal, or button "arrow back" like activity)

  • Use $route.go(-1); for all
  • Or manual $route.replace({name: first}); and $route.replace('/'); respectively

Ok. But if I paste or type URL before? _vue-router_ it may?

What does the proposed API look like?

_vue-router_ has $route.back() like alias for $route.go(-1); This does not work if there is no history.
I need to do my own thing?.. But.. I do the following

{ path: '/', name: 'root', component: RootActivity, meta: {back: 'searchSettings'} },
{ path: '/first', name: 'first', component: FirstActivity, meta: {back: 'RootActivity'} },
{ path: '/second', name: 'second', component: SecondActivity, meta: {back: 'SecondActivity'} },

And in my click button "back method":

var {back} = this.$route.meta;
if(back) {
    this.$router.replace({name: back});
} else {
    // When no one knows where to go
    this.$router.replace('/'); 
}

I think this is standard behavior. Used often. You could make the method "back" smarter like this (no use meta)

{ path: '/second', name: 'second', component: SecondActivity, back: nameOfRouteOrPathOrObject },

And the "back" method will simply look, whether there is a record in the routing or not. In #575 You said that this is about the local state. But it seems to me that this is about routing...

Most helpful comment

Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server, gitter or StackOverflow.


Use router.go(-1)

All 5 comments

Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server, gitter or StackOverflow.


Use router.go(-1)

Ok. I will repeat :stuck_out_tongue:

This does not work if there is no history. I need to do my own thing?..
In #575 You said that this is about the local state. But it seems to me that this is about routing...

Probably routing is not only going forward.

In order to go back, we also need a functional.

When there is no history in the browser, the router does _not have the ability_ to go back normally.

yeah, you'll need to use plain javascript for that. Please, use the forum, discord or so next time for questions 馃檪

@tebaly Thank you for the example by this.$route.meta

Hi,
for me i solve this issue as following

niceback: function(){
    var numberOfEntries = window.history.length;
    if(numberOfEntries>2){
      this.$router.go(-1)
    }else{
    var fpath=  this.PageData.backCrumb.url;
    this.$router.push({
      path: fpath
  })
    }

  },

hope it will help you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

posva picture posva  路  3Comments

thomas-alrek picture thomas-alrek  路  3Comments

rr326 picture rr326  路  3Comments

kerlor picture kerlor  路  3Comments

Atinux picture Atinux  路  3Comments