Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: route code to move the redirect of an empty node to the parent node #1116

Open
wants to merge 4 commits into
base: refactor/develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions packages/vue-generator/src/plugins/genRouterPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,28 @@ const flattenRoutes = (routes, parentPath = '') => {
return routes.reduce((acc, route) => {
const fullPath = `${parentPath}${route.path}`

if (route.path !== '') {
if (route.component) {
// 如果存在 component,则直接添加路由
const newRoute = {
name: `${route.name}`,
path: fullPath,
component: route.component,
children: flattenRoutes(route.children)
}
const redirectChild = route.children.find((item) => item.isDefault)

if (route.children && redirectChild) {
newRoute.redirect = { name: `${redirectChild.name}` }
}
if (route.component) {
// 如果存在 component,则直接添加路由
const newRoute = {
name: `${route.name}`,
path: fullPath,
component: route.component,
children: flattenRoutes(route.children)
}
const redirectChild = route.children.find((item) => item.isDefault)

acc.push(newRoute)
} else if (route.children && route.children.length > 0) {
// 如果不存在 component 但有 children,则递归处理 children
const children = flattenRoutes(route.children, fullPath + '/')
// 将处理后的 children 合并到上一层存在 component 的路由中
acc.push(...children)
if (route.children && redirectChild) {
newRoute.redirect = { name: `${redirectChild.name}` }
}
// 如果既没有 component 也没有 children,则不做任何处理
} else {
acc.push(route)

acc.push(newRoute)
} else if (route.children && route.children.length > 0) {
// 如果不存在 component 但有 children,则递归处理 children
const children = flattenRoutes(route.children, fullPath + '/')
// 将处理后的 children 合并到上一层存在 component 的路由中
acc.push(...children)
}
// 如果既没有 component 也没有 children,则不做任何处理

return acc
}, [])
Expand All @@ -49,7 +45,7 @@ const convertToNestedRoutes = (schema) => {
pageSchema.forEach((item) => {
if ((item.meta?.isHome || item.meta?.isDefault) && !isGetHome) {
home = {
path: '',
path: '/',
redirect: { name: `${item.meta.id}` }
}
isGetHome = true
Expand Down Expand Up @@ -90,10 +86,11 @@ const convertToNestedRoutes = (schema) => {
})

if (home.redirect) {
result.unshift(home)
home.children = flattenRoutes(result)
return home
} else {
return flattenRoutes(result)
}

return flattenRoutes(result)
}

// 示例路由数组
Expand Down Expand Up @@ -121,7 +118,7 @@ function genRouterPlugin(options = {}) {
const exportSnippet = `
export default createRouter({
history: createWebHashHistory(),
routes: [{path: '/',children: routes}]
routes: routes
lichunn marked this conversation as resolved.
Show resolved Hide resolved
})`

const routeSnippets = `const routes = ${resultStr}`
Expand Down