How can I go back/route-back on vue-router?
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>
Use $router.back()
directly to go back/route-back programmatic on vue-router. Or if in the template of the component use router.back()
.
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">« Back</button>