Skip to content

Commit 364cd04

Browse files
committed
feat(dx): warn when addRoute cannot find the parent
1 parent 13303bd commit 364cd04

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/router/__tests__/router.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1076,5 +1076,22 @@ describe('Router', () => {
10761076
name: 'Param',
10771077
})
10781078
})
1079+
1080+
it('warns when the parent route is missing', async () => {
1081+
const { router } = await newRouter()
1082+
router.addRoute('parent-route', {
1083+
path: '/p',
1084+
component: components.Foo,
1085+
})
1086+
expect(
1087+
'Parent route "parent-route" not found when adding child route'
1088+
).toHaveBeenWarned()
1089+
})
1090+
1091+
it('warns when removing a missing route', async () => {
1092+
const { router } = await newRouter()
1093+
router.removeRoute('route-name')
1094+
expect('Cannot remove non-existent route "route-name"').toHaveBeenWarned()
1095+
})
10791096
})
10801097
})

packages/router/src/router.ts

+8
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ export function createRouter(options: RouterOptions): Router {
397397
let record: RouteRecordRaw
398398
if (isRouteName(parentOrRoute)) {
399399
parent = matcher.getRecordMatcher(parentOrRoute)
400+
if (__DEV__ && !parent) {
401+
warn(
402+
`Parent route "${String(
403+
parentOrRoute
404+
)}" not found when adding child route`,
405+
route
406+
)
407+
}
400408
record = route!
401409
} else {
402410
record = parentOrRoute

0 commit comments

Comments
 (0)