@esmx/router-vue 3.0.0-rc.103

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +570 -0
  3. package/README.zh-CN.md +570 -0
  4. package/dist/index.d.ts +6 -0
  5. package/dist/index.mjs +13 -0
  6. package/dist/index.test.d.ts +1 -0
  7. package/dist/index.test.mjs +216 -0
  8. package/dist/plugin.d.ts +61 -0
  9. package/dist/plugin.mjs +41 -0
  10. package/dist/plugin.test.d.ts +1 -0
  11. package/dist/plugin.test.mjs +631 -0
  12. package/dist/router-link.d.ts +220 -0
  13. package/dist/router-link.mjs +119 -0
  14. package/dist/router-link.test.d.ts +1 -0
  15. package/dist/router-link.test.mjs +663 -0
  16. package/dist/router-view.d.ts +31 -0
  17. package/dist/router-view.mjs +15 -0
  18. package/dist/router-view.test.d.ts +1 -0
  19. package/dist/router-view.test.mjs +676 -0
  20. package/dist/run-with-context.test.d.ts +1 -0
  21. package/dist/run-with-context.test.mjs +57 -0
  22. package/dist/use.d.ts +260 -0
  23. package/dist/use.mjs +125 -0
  24. package/dist/use.test.d.ts +1 -0
  25. package/dist/use.test.mjs +381 -0
  26. package/dist/util.d.ts +20 -0
  27. package/dist/util.mjs +49 -0
  28. package/dist/util.test.d.ts +4 -0
  29. package/dist/util.test.mjs +604 -0
  30. package/dist/vue2.d.ts +15 -0
  31. package/dist/vue2.mjs +0 -0
  32. package/dist/vue3.d.ts +13 -0
  33. package/dist/vue3.mjs +0 -0
  34. package/package.json +85 -0
  35. package/src/index.test.ts +273 -0
  36. package/src/index.ts +15 -0
  37. package/src/plugin.test.ts +812 -0
  38. package/src/plugin.ts +107 -0
  39. package/src/router-link.test.ts +830 -0
  40. package/src/router-link.ts +172 -0
  41. package/src/router-view.test.ts +840 -0
  42. package/src/router-view.ts +59 -0
  43. package/src/run-with-context.test.ts +64 -0
  44. package/src/use.test.ts +484 -0
  45. package/src/use.ts +416 -0
  46. package/src/util.test.ts +760 -0
  47. package/src/util.ts +85 -0
  48. package/src/vue2.ts +18 -0
  49. package/src/vue3.ts +15 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { RouterLink } from './router-link';
2
+ import { RouterView } from './router-view';
3
+ import { getRoute, getRouter } from './use';
4
+ import { defineRouterProperties, isVue2 } from './util';
5
+
6
+ interface VueApp {
7
+ config?: {
8
+ globalProperties: Record<string, unknown>;
9
+ };
10
+ prototype?: Record<string, unknown>;
11
+ component(name: string, component: unknown): void;
12
+ }
13
+
14
+ /**
15
+ * Vue plugin for \@esmx/router integration.
16
+ * Registers RouterLink and RouterView components globally.
17
+ * Compatible with both Vue 2.7+ and Vue 3.
18
+ *
19
+ * @example
20
+ *
21
+ * Vue 3 installation
22
+ *
23
+ * ```typescript
24
+ * import { createApp } from 'vue';
25
+ * import { Router } from '@esmx/router';
26
+ * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
27
+ *
28
+ * const routes = [
29
+ * { path: '/', component: Home },
30
+ * { path: '/about', component: About }
31
+ * ];
32
+ *
33
+ * const router = new Router({ routes });
34
+ * const app = createApp({
35
+ * setup() {
36
+ * useProvideRouter(router);
37
+ * }
38
+ * });
39
+ *
40
+ * app.use(RouterPlugin);
41
+ * app.mount('#app');
42
+ * ```
43
+ *
44
+ * @example
45
+ *
46
+ * Vue 2 installation
47
+ *
48
+ * ```typescript
49
+ * import Vue from 'vue';
50
+ * import { Router } from '@esmx/router';
51
+ * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
52
+ *
53
+ * const routes = [
54
+ * { path: '/', component: Home },
55
+ * { path: '/about', component: About }
56
+ * ];
57
+ *
58
+ * const router = new Router({ routes });
59
+ * Vue.use(RouterPlugin);
60
+ *
61
+ * new Vue({
62
+ * setup() {
63
+ * useProvideRouter(router);
64
+ * }
65
+ * }).$mount('#app');
66
+ * ```
67
+ */
68
+ export const RouterPlugin = {
69
+ /**
70
+ * Install the router plugin.
71
+ * @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
72
+ */
73
+ install(app: unknown): void {
74
+ if (!app) {
75
+ throw new Error('[@esmx/router-vue] Invalid Vue app instance');
76
+ }
77
+
78
+ const vueApp = app as VueApp;
79
+ const target = vueApp.config?.globalProperties || vueApp.prototype;
80
+
81
+ if (!target) {
82
+ throw new Error('[@esmx/router-vue] Invalid Vue app instance');
83
+ }
84
+ if (isVue2) {
85
+ defineRouterProperties(
86
+ target,
87
+ function (this: any) {
88
+ return getRouter(this);
89
+ },
90
+ function (this: any) {
91
+ return getRoute(this);
92
+ }
93
+ );
94
+ } else {
95
+ const throwError = () => {
96
+ throw new Error(
97
+ '[@esmx/router-vue] Router not provided. Please call useProvideRouter() in your root component setup.'
98
+ );
99
+ };
100
+ defineRouterProperties(target, throwError, throwError, true);
101
+ }
102
+
103
+ // Register global components
104
+ vueApp.component('RouterLink', RouterLink);
105
+ vueApp.component('RouterView', RouterView);
106
+ }
107
+ };