]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: fix type in navigation guard
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 15 Mar 2020 18:58:43 +0000 (19:58 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 15 Mar 2020 18:58:43 +0000 (19:58 +0100)
src/navigationGuards.ts

index 97eecc8ff12c3a6458ba54455b554e8be66c1539..515fd3e945f2a592bd91dac82036e54bcb9e384f 100644 (file)
@@ -1,6 +1,7 @@
 import { NavigationGuard } from './types'
 import { inject, getCurrentInstance, warn } from 'vue'
 import { matchedRouteKey } from './components/View'
+import { RouteRecordNormalized } from './matcher/types'
 
 export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
   const instance = getCurrentInstance()
@@ -10,15 +11,19 @@ export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
     return
   }
 
-  // TODO: fix wrong type
-  const matched = inject(matchedRouteKey, {} as any).value
+  const activeRecord: RouteRecordNormalized | undefined = inject(
+    matchedRouteKey,
+    {} as any
+  ).value
 
-  if (!matched) {
+  if (!activeRecord) {
     __DEV__ &&
       warn('onRouteLeave must be called at the top of a setup function')
     return
   }
 
-  // @ts-ignore
-  matched.leaveGuards.push(leaveGuard.bind(instance.proxy))
+  activeRecord.leaveGuards.push(
+    // @ts-ignore do we even want to allow that? Passing the context in a composition api hook doesn't make sense
+    leaveGuard.bind(instance.proxy)
+  )
 }