Skip to content
Merged
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
26 changes: 7 additions & 19 deletions src/vue-router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { computed, ComputedRef, getCurrentInstance, reactive, shallowRef } from '@vue/composition-api'
import { getCurrentInstance, reactive } from '@vue/composition-api'
import VueRouter, { NavigationGuard, Route, RouterOptions } from 'vue-router'
import { OUT_OF_SCOPE, warn } from './utils'

Expand Down Expand Up @@ -61,25 +61,13 @@ let currentRoute: RouteLocationNormalizedLoaded
export function useRoute() {
const router = useRouter()
if (!currentRoute) {
const routeRef = shallowRef({
path: '/',
name: undefined,
params: {},
query: {},
hash: '',
fullPath: '/',
matched: [],
meta: {},
redirectedFrom: undefined,
} as Route);
const computedRoute = {} as {
[key in keyof Route]: ComputedRef<Route[key]>
const inst = getCurrentInstance()
if (!inst) {
warn(OUT_OF_SCOPE)
return
}
for (const key of Object.keys(routeRef.value) as (keyof Route)[]) {
computedRoute[key] = computed<any>(() => routeRef.value[key])
}
router.afterEach(to => routeRef.value = to)
currentRoute = reactive(computedRoute)
currentRoute = reactive({...inst.proxy.$route} as Route)
router.afterEach(to => Object.assign(currentRoute, to))
}
return currentRoute
}
Expand Down