Skip to content

Commit c223ebd

Browse files
committed
feat: Expose useRoute and useRouter for future compatibility with vue 3
1 parent 3f44b15 commit c223ebd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRouter } from "./vue-router.common";
1+
import { createRouter, useRouter, useRoute } from "./vue-router.common";
22
export { Route, RouteOptions, RouterServiceOptions } from "./typings/router-service";
33

4-
export { createRouter };
4+
export { createRouter, useRouter, useRoute };

src/vue-router.common.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { registerActionDispatcher } from "./router-dispatcher-service";
1212

1313
import routerMixin from "./router-mixin";
1414

15+
const routers = [] as RouterService[];
16+
1517
/**
1618
* Create router wrapper function
1719
*
@@ -32,11 +34,15 @@ export const createRouter = (
3234
...routerOptions,
3335
});
3436

37+
routers.push(router);
38+
3539
// Vue 3 compatibility
3640
if (vm.createApp && vm.config.globalProperties) {
3741
vm.config.globalProperties.$routeTo = router.push.bind(router);
3842
vm.config.globalProperties.$routeBack = router.back.bind(router);
3943
vm.config.globalProperties.$router = router;
44+
45+
vm.provide('$router', router);
4046
} else {
4147
proto.$routeTo = router.push.bind(router);
4248
proto.$routeBack = router.back.bind(router);
@@ -62,6 +68,14 @@ export const createRouter = (
6268
return router;
6369
};
6470

71+
export const useRouter = (routerIndex = 0) => {
72+
return routers[routerIndex];
73+
};
74+
75+
export const useRoute = (routerIndex = 0) => {
76+
return routers[routerIndex].getCurrentRoute();
77+
};
78+
6579
export default {
6680
createRouter,
6781
};

0 commit comments

Comments
 (0)