]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(dx): warn when `addRoute` cannot find the parent (#2157)
authorskirtle <65301168+skirtles-code@users.noreply.github.com>
Sun, 3 Mar 2024 09:01:47 +0000 (09:01 +0000)
committerGitHub <noreply@github.com>
Sun, 3 Mar 2024 09:01:47 +0000 (10:01 +0100)
packages/router/__tests__/router.spec.ts
packages/router/src/router.ts

index 951f2f0db829454f05a1a241b8c2deb3a3b055fb..e762971b5bf32c00af816497a3273f25c5b8b068 100644 (file)
@@ -1076,5 +1076,22 @@ describe('Router', () => {
         name: 'Param',
       })
     })
+
+    it('warns when the parent route is missing', async () => {
+      const { router } = await newRouter()
+      router.addRoute('parent-route', {
+        path: '/p',
+        component: components.Foo,
+      })
+      expect(
+        'Parent route "parent-route" not found when adding child route'
+      ).toHaveBeenWarned()
+    })
+
+    it('warns when removing a missing route', async () => {
+      const { router } = await newRouter()
+      router.removeRoute('route-name')
+      expect('Cannot remove non-existent route "route-name"').toHaveBeenWarned()
+    })
   })
 })
index b1ec39dfd3fa7e65fef336bc32aa1cea5274ea59..8fec39d82282941f69f7d52dbad1251a9f977a1f 100644 (file)
@@ -397,6 +397,14 @@ export function createRouter(options: RouterOptions): Router {
     let record: RouteRecordRaw
     if (isRouteName(parentOrRoute)) {
       parent = matcher.getRecordMatcher(parentOrRoute)
+      if (__DEV__ && !parent) {
+        warn(
+          `Parent route "${String(
+            parentOrRoute
+          )}" not found when adding child route`,
+          route
+        )
+      }
       record = route!
     } else {
       record = parentOrRoute