Skip to content

Is there a way to do a redirect? #63

Closed Answered by posva
phppirate asked this question in Q&A
Discussion options

You must be logged in to vote

Use extendRoutes to add the redirect option:

const router = createRouter({
  extendRoutes: (routes) => {
    const targetRoute = routes.find((r) => r.name === '/')
    if (targetRoute) {
      targetRoute.redirect = { name: 'dashboard' }
    }
    // completely optional since we are modifying the routes in place
    return routes
  },
  history: createWebHistory(),
})

Since the redirect option only exist for routes without a component or with children, it's a different type of option. Maybe we could find a way of improving this. The problem when adding redirect to the <route block> is that it still forces to add other options if the route doesn't have children 🤔

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@phppirate
Comment options

@posva
Comment options

@phppirate
Comment options

@posva
Comment options

Answer selected by phppirate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants