fix(router): show default child if trailingSlash is false (#6594)

This commit is contained in:
Alexander Lichter 2020-07-09 15:08:12 +02:00 committed by GitHub
parent 5272a48ea9
commit c5465e65c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -30,7 +30,7 @@ export const flatRoutes = function flatRoutes (router, fileName = '', routes = [
return routes
}
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-') {
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-', trailingSlash) {
let start = -1
const regExpIndex = new RegExp(`${routeNameSplitter}index$`)
const routesIndex = []
@ -68,10 +68,15 @@ function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-')
}
route.name = route.name.replace(regExpIndex, '')
if (route.children) {
if (route.children.find(child => child.path === '')) {
const indexRoutePath = trailingSlash === false ? '/' : ''
const defaultChildRoute = route.children.find(child => child.path === indexRoutePath)
if (defaultChildRoute) {
if (trailingSlash === false) {
defaultChildRoute.name = route.name
}
delete route.name
}
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter)
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter, trailingSlash)
}
})
return routes
@ -188,7 +193,7 @@ export const createRoutes = function createRoutes ({
})
sortRoutes(routes)
return cleanChildrenRoutes(routes, false, routeNameSplitter)
return cleanChildrenRoutes(routes, false, routeNameSplitter, trailingSlash)
}
// Guard dir1 from dir2 which can be indiscriminately removed

View File

@ -260,7 +260,6 @@ Array [
],
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
"name": "another_route-id",
"path": "/another_route/:id?",
"pathToRegexpOptions": Object {
"strict": true,