Skip to content

Commit ddf5a29

Browse files
committed
对未设置 redirect 的父路由,访问时将自动重定向到允许访问的子路由
1 parent d572e6d commit ddf5a29

18 files changed

+33
-38
lines changed

src/mock/app.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default defineFakeRoute([
1818
{
1919
path: '/multilevel_menu_example',
2020
component: 'Layout',
21-
redirect: '/multilevel_menu_example/page',
2221
name: 'multilevelMenuExample',
2322
meta: {
2423
title: '多级导航',
@@ -36,7 +35,6 @@ export default defineFakeRoute([
3635
{
3736
path: 'level2',
3837
name: 'multilevelMenuExample2',
39-
redirect: '/multilevel_menu_example/level2/page',
4038
meta: {
4139
title: '导航2',
4240
},
@@ -52,7 +50,6 @@ export default defineFakeRoute([
5250
{
5351
path: 'level3',
5452
name: 'multilevelMenuExample2-2',
55-
redirect: '/multilevel_menu_example/level2/level3/page1',
5653
meta: {
5754
title: '导航2-2',
5855
},
@@ -82,7 +79,6 @@ export default defineFakeRoute([
8279
{
8380
path: '/permission_example',
8481
component: 'Layout',
85-
redirect: '/permission_example/index',
8682
name: 'permissionExample',
8783
meta: {
8884
title: '权限验证',

src/router/guards.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@ function setupRoutes(router: Router) {
112112
})
113113
}
114114

115+
// 当父级路由未配置重定向时,自动重定向到有访问权限的子路由
116+
function setupRedirectAuthChildendRoute(router: Router) {
117+
router.beforeEach((to, _from, next) => {
118+
const { auth } = useAuth()
119+
const currentRoute = router.getRoutes().find(route => route.path === (to.matched.at(-1)?.path ?? ''))
120+
if (!currentRoute?.redirect) {
121+
const findAuthRoute = currentRoute?.children?.find(route => route.meta?.menu !== false && auth(route.meta?.auth ?? ''))
122+
if (findAuthRoute) {
123+
next({
124+
path: findAuthRoute.path,
125+
replace: true,
126+
})
127+
}
128+
else {
129+
next()
130+
}
131+
}
132+
else {
133+
next()
134+
}
135+
})
136+
}
137+
115138
// 进度条
116139
function setupProgress(router: Router) {
117140
const { isLoading } = useNProgress()
@@ -208,6 +231,7 @@ function setupOther(router: Router) {
208231

209232
export default function setupGuards(router: Router) {
210233
setupRoutes(router)
234+
setupRedirectAuthChildendRoute(router)
211235
setupProgress(router)
212236
setupTitle(router)
213237
setupKeepAlive(router)

src/router/modules/breadcrumb.example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/breadcrumb_example',
99
component: Layout,
10-
redirect: '/breadcrumb_example/list1',
1110
name: 'breadcrumbExample',
1211
meta: {
1312
title: '面包屑导航',
@@ -34,7 +33,6 @@ const routes: RouteRecordRaw = {
3433
},
3534
{
3635
path: 'list2',
37-
redirect: '/breadcrumb_example/list2',
3836
meta: {
3937
title: '列表2(层级模式)',
4038
},

src/router/modules/component.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ const routes: RouteRecordRaw = {
242242
},
243243
{
244244
path: 'extend',
245-
redirect: 'pageheader',
246245
name: 'componentExampleExtend',
247246
meta: {
248247
title: '扩展组件',

src/router/modules/ecology.example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw[] = [
88
{
99
path: '/official',
10-
redirect: '/official/fantastic-startkit',
1110
component: Layout,
1211
meta: {
1312
title: '官方周边',
@@ -48,7 +47,6 @@ const routes: RouteRecordRaw[] = [
4847
},
4948
{
5049
path: '/recommand',
51-
redirect: '/recommand/vform',
5250
component: Layout,
5351
meta: {
5452
title: '友情推荐',

src/router/modules/external.link.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/link',
99
component: Layout,
10-
redirect: '/link/gitee',
1110
name: 'externalLinkExample',
1211
meta: {
1312
title: '外链',

src/router/modules/feature.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/feature_example',
99
component: Layout,
10-
redirect: '/feature_example/clipboard',
1110
name: 'featureExample',
1211
meta: {
1312
title: '功能',

src/router/modules/icon.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/icon_example',
99
component: Layout,
10-
redirect: '/icon_example/svg',
1110
name: 'iconExample',
1211
meta: {
1312
title: '扩展图标',

src/router/modules/jsx.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/jsx_example',
99
component: Layout,
10-
redirect: '/jsx_example/index',
1110
name: 'jsxExample',
1211
meta: {
1312
title: 'JSX',

src/router/modules/keep.alive.example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/keep_alive_example',
99
component: Layout,
10-
redirect: '/keep_alive_example/page',
1110
name: 'keepAliveExample',
1211
meta: {
1312
title: '页面缓存',
@@ -35,7 +34,6 @@ const routes: RouteRecordRaw = {
3534
},
3635
{
3736
path: 'nested',
38-
redirect: '/keep_alive_example/nested/detail',
3937
meta: {
4038
title: '嵌套路由',
4139
menu: false,

0 commit comments

Comments
 (0)