Nishank Sisodiya
Nishank Sisodiya

Reputation: 170

Vue router go back to previous parent route

My vue app have Settings page with child routes like settings/user, settings/addUser, etc. what i want to do is implement a back button such that when user press it they go back to the page they visited settings this.$router.go(-1) seem to take me back one page which is not always ideal

for now have

<v-btn class="my-2" small fab rounded color="primary" block :to="/home">
  <v-icon color="black">
    mdi-home-export-outline mdi-flip-h
  </v-icon>
</v-btn>

this is in settings.vue where <router-view> is present for child routes to render

Upvotes: 3

Views: 2209

Answers (1)

glass-half-fullstack
glass-half-fullstack

Reputation: 61

I wonder if Navigation Guards would do the trick? If they are routed back to the home page you can check if they are admin and redirect them to their correct home page from inside the Navigation Guard.

Here is the example from the website I linked:

router.beforeEach( (to, next, from) => {
  if (to.path === '/profile') {
    if (!hasAccess(to.path)) { // just some arbitrary conditional
	    next(false) // deny access to this page
    } else {
	    next() // keep moving on to next hook
    }
  } else {
    next() // keep moving on to next hook
  }
})

Upvotes: 1

Related Questions