Fix useRoute helper

This commit is contained in:
Matthias 2022-08-10 20:13:32 +02:00
parent 52d62f7801
commit ff6e1d3ddb

View File

@ -1,14 +1,34 @@
// TODO: This helper can be removed once
// vue-router either releases a new version, or we update to vue3.
import { getCurrentInstance } from 'vue';
import { effectScope, getCurrentInstance, reactive } from 'vue';
export function useRoute() {
import { Route } from 'vue-router';
let currentRoute: Route;
function assign(target: Record<string, any>, source: Record<string, any>) {
for (const key of Object.keys(source)) {
target[key] = source[key];
}
return target;
}
export function useRoute(): Route {
const inst = getCurrentInstance();
if (!inst) {
throw new Error('No current instance found');
return undefined as any;
}
const { proxy } = inst;
return proxy.$route;
if (!currentRoute) {
const scope = effectScope(true);
scope.run(() => {
const { $router } = inst.proxy;
currentRoute = reactive(assign({}, $router.currentRoute)) as any;
$router.afterEach((to) => {
assign(currentRoute, to);
});
});
}
return currentRoute;
}
export function useRouter() {
const inst = getCurrentInstance();