mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-18 14:41:25 +00:00
fix(router): optional dynamic routing in children routes (#7843)
This commit is contained in:
parent
c16d199f0f
commit
ec534d05b9
@ -41,29 +41,26 @@ export const flatRoutes = function flatRoutes (router, fileName = '', routes = [
|
|||||||
return routes
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-', trailingSlash) {
|
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-', trailingSlash, parentRouteName) {
|
||||||
let start = -1
|
|
||||||
const regExpIndex = new RegExp(`${routeNameSplitter}index$`)
|
const regExpIndex = new RegExp(`${routeNameSplitter}index$`)
|
||||||
|
const regExpParentRouteName = new RegExp(`^${parentRouteName}${routeNameSplitter}`)
|
||||||
const routesIndex = []
|
const routesIndex = []
|
||||||
routes.forEach((route) => {
|
routes.forEach((route) => {
|
||||||
if (regExpIndex.test(route.name) || route.name === 'index') {
|
if (regExpIndex.test(route.name) || route.name === 'index') {
|
||||||
// Save indexOf 'index' key in name
|
const res = route.name.replace(regExpParentRouteName, '').split(routeNameSplitter)
|
||||||
const res = route.name.split(routeNameSplitter)
|
|
||||||
const s = res.indexOf('index')
|
|
||||||
start = start === -1 || s < start ? s : start
|
|
||||||
routesIndex.push(res)
|
routesIndex.push(res)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
routes.forEach((route) => {
|
routes.forEach((route) => {
|
||||||
route.path = isChild ? route.path.replace('/', '') : route.path
|
route.path = isChild ? route.path.replace('/', '') : route.path
|
||||||
if (route.path.includes('?')) {
|
if (route.path.includes('?')) {
|
||||||
const names = route.name.split(routeNameSplitter)
|
const names = route.name.replace(regExpParentRouteName, '').split(routeNameSplitter)
|
||||||
const paths = route.path.split('/')
|
const paths = route.path.split('/')
|
||||||
if (!isChild) {
|
if (!isChild) {
|
||||||
paths.shift()
|
paths.shift()
|
||||||
} // clean first / for parents
|
} // clean first / for parents
|
||||||
routesIndex.forEach((r) => {
|
routesIndex.forEach((r) => {
|
||||||
const i = r.indexOf('index') - start // children names
|
const i = r.indexOf('index')
|
||||||
if (i < paths.length) {
|
if (i < paths.length) {
|
||||||
for (let a = 0; a <= i; a++) {
|
for (let a = 0; a <= i; a++) {
|
||||||
if (a === i) {
|
if (a === i) {
|
||||||
@ -81,13 +78,14 @@ function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-',
|
|||||||
if (route.children) {
|
if (route.children) {
|
||||||
const indexRoutePath = trailingSlash === false ? '/' : ''
|
const indexRoutePath = trailingSlash === false ? '/' : ''
|
||||||
const defaultChildRoute = route.children.find(child => child.path === indexRoutePath)
|
const defaultChildRoute = route.children.find(child => child.path === indexRoutePath)
|
||||||
|
const routeName = route.name
|
||||||
if (defaultChildRoute) {
|
if (defaultChildRoute) {
|
||||||
if (trailingSlash === false) {
|
if (trailingSlash === false) {
|
||||||
defaultChildRoute.name = route.name
|
defaultChildRoute.name = route.name
|
||||||
}
|
}
|
||||||
delete route.name
|
delete route.name
|
||||||
}
|
}
|
||||||
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter, trailingSlash)
|
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter, trailingSlash, routeName)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return routes
|
return routes
|
||||||
|
Loading…
Reference in New Issue
Block a user