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

Merged
merged 6 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 26 additions & 34 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 @@ -43,15 +39,14 @@ const flattenRoutes = (routes, parentPath = '') => {
const convertToNestedRoutes = (schema) => {
const pageSchema = (schema.pageSchema || []).sort((a, b) => a.meta?.router?.length - b.meta?.router?.length)
const result = []
let home = {}
let home = {
path: '/'
}
let isGetHome = false

pageSchema.forEach((item) => {
if ((item.meta?.isHome || item.meta?.isDefault) && !isGetHome) {
home = {
path: '',
redirect: { name: `${item.meta.id}` }
}
home.redirect = { name: `${item.meta.id}` }
isGetHome = true
}

Expand Down Expand Up @@ -89,11 +84,8 @@ const convertToNestedRoutes = (schema) => {
})
})

if (home.redirect) {
result.unshift(home)
}

return flattenRoutes(result)
home.children = flattenRoutes(result)
return [home]
}

// 示例路由数组
Expand Down Expand Up @@ -121,7 +113,7 @@ function genRouterPlugin(options = {}) {
const exportSnippet = `
export default createRouter({
history: createWebHashHistory(),
routes: [{path: '/',children: routes}]
routes
})`

const routeSnippets = `const routes = ${resultStr}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
{
name: '5bhD7p5FUsUOTFRN',
path: 'demopage',
component: () => import('@/views/DemoPage.vue'),
children: []
path: '/',
children: [
{
name: '5bhD7p5FUsUOTFRN',
path: 'demopage',
component: () => import('@/views/DemoPage.vue'),
children: []
}
]
}
]

export default createRouter({
history: createWebHashHistory(),
routes: [{ path: '/', children: routes }]
routes
})
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
{
name: '5bhD7p5FUsUOTFRN',
path: 'demopage',
component: () => import('@/views/DemoPage.vue'),
children: []
},
{
name: 'NTJ4MjvqoVj8OVsc',
path: 'createVm',
component: () => import('@/views/createVm.vue'),
children: []
},
{
name: '1737797330916',
path: 'testCanvasRowCol',
component: () => import('@/views/testCanvasRowCol.vue'),
children: []
path: '/',
redirect: {
name: 'NTJ4MjvqoVj8OVsc'
},
children: [
{
name: '5bhD7p5FUsUOTFRN',
path: 'demopage',
component: () => import('@/views/DemoPage.vue'),
children: []
},
{
name: 'NTJ4MjvqoVj8OVsc',
path: 'createVm',
component: () => import('@/views/createVm.vue'),
children: []
},
{
name: '1737797330916',
path: 'testCanvasRowCol',
component: () => import('@/views/testCanvasRowCol.vue'),
children: []
}
]
}
]

export default createRouter({
history: createWebHashHistory(),
routes: [{ path: '/', children: routes }]
routes
})
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ export const appSchemaDemo01 = {
confirmationToken: 'dfb2c162-351f-4f44-ad5f-8998',
is_admin: true
},
isHome: false,
isHome: true,
_id: 'NTJ4MjvqoVj8OVsc'
}
},
Expand Down