vuejs / router

🚦 The official router for Vue.js
https://router.vuejs.org/
MIT License
3.95k stars 1.19k forks source link

redirect function add a [children] param at runtime #1993

Closed genffy closed 1 year ago

genffy commented 1 year ago

What problem is this solving

the scenario is the nest children routers have some biz logics to filter it such as menu auth. but now Redirct function params only have a to param.

Proposed solution

expect the redirect function add a children param for dynamic reach it nest children routes.

Describe alternatives you've considered

No response

genffy commented 1 year ago
redirect: ({ matched }) => {
  if (matched && matched.length > 0) {
    if (matched[0].children && matched[0].children.length > 0) {
      return matched[0].children[0]
    }
  }
 // error page
  return 'NoFind'
}
posva commented 1 year ago

BTW: You can probably only return the name if there is one, it's safer than returning the record as otherwise it would fallback to checking the path and if it contains params it will end up redirecting to something like /path/:id

redirect: ({ matched }) => {
  if (matched && matched.length > 0) {
    if (matched[0].children && matched[0].children.length > 0) {
      return { name: matched[0].children[0].name }
    }
  }
 // error page
  return 'NoFind'
}