Question: I want to get previous page link or url in `vue-router`. Like a this. How to do it?

```
const link = this.$router.getPrevLink(); // function is not exist?
this.$router.push(link);

```

Near concept is `this.$router.go(-1)`.

```
this.$router.go(-1);

```

Answer: 
For Vue 3 with Vue Router 4, my solution was:

```
this.$router.options.history.state.back

```

The final code:

```
export default defineComponent({
  name: 'Error404',
  data () {
    return {
      lastPath: null
    }
  },
  created () {
    this.lastPath = this.$router.options.history.state.back
  },
  computed: {
    prevRoutePatch () {
      return this.lastPath ? this.lastPath : '/dashboard'
    }
  }
})

```

Regards.
Though [this answer](https://stackoverflow.com/a/53789212/9764426) is great, *the received route wouldn't always be the route before in history* in case of popstate navigations (aka when the user clicks the back button).

So **if you use Vuex** here's a code snippet that respects this behavior:

```js
const store = new Vuex.Store({
  state: {
    routerHistory: [],
  },
});

const router = new VueRouter({
  mode: 'history',
  scrollBehavior(to, from, savedPosition) {
    const fromHistory = Boolean(savedPosition);

    if (fromHistory && store.state.routerHistory.length > 0) {
      store.state.routerHistory.splice(-1, 1);
    } else {
      store.state.routerHistory.push(from);
    }

    return savedPosition || { x: 0, y: 0 };
  },
});

```

Once implemented, you can define a getter for the previous route inside your store:

```
const store = new Vuex.Store({
  // ...
  getters: {
    previousRoute: (state) => {
      const historyLen = state.routerHistory.length;
      if (historyLen == 0) return null;
      return state.routerHistory[historyLen - 1];
    },
  },
});

```

This code uses the scrollBehavior hook, which only receives the savedPosition argument if it was a popstate navigation.
Thanks to Vuex we can then store all the routes in an array (over multiple pages).