Skip to content

Catch cancelled navigations #2012

Open
Open
@posva

Description

@posva

What problem does this feature solve?

This will allow users to hook on:

  • redirections to the current route (could be a different hook like onRedirect, also triggered by redirect option)
  • next(false) calls
  • navigation delayed and replaced by other navigations:
    • eg: users click when async component not finished loading

I'm a bit sceptical about use cases on why to hook on redirects, so I'll love to read if you have usecases

What does the proposed API look like?

router.onCancel((to, from, currentLocation) => {
  // from -> original route where the navigation was triggered
  // to -> where was it supposed to go
  // currentLocation -> location where we ended up (same as this.$route in components and router.currentRouter
})

can be triggered by

router.beforeEach((to, from, next) => {
  next(false) // cancel navigation explicitely
})

router.beforeEach((to, from, next) => {
  to.fullPath // /foo
  from.fullPath // /home
  // redirect to /home if we're on /foo
  // this will do nothing if we are already on /home
  if (to.fullPath === '/foo') next('/home')
  else next()
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestfixed on 4.xThis issue has been already fixed on the v4 but exists in v3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions