@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.
- package/LICENSE +21 -0
- package/README.md +570 -0
- package/README.zh-CN.md +570 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +13 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.mjs +216 -0
- package/dist/plugin.d.ts +61 -0
- package/dist/plugin.mjs +41 -0
- package/dist/plugin.test.d.ts +1 -0
- package/dist/plugin.test.mjs +631 -0
- package/dist/router-link.d.ts +220 -0
- package/dist/router-link.mjs +119 -0
- package/dist/router-link.test.d.ts +1 -0
- package/dist/router-link.test.mjs +663 -0
- package/dist/router-view.d.ts +31 -0
- package/dist/router-view.mjs +15 -0
- package/dist/router-view.test.d.ts +1 -0
- package/dist/router-view.test.mjs +676 -0
- package/dist/run-with-context.test.d.ts +1 -0
- package/dist/run-with-context.test.mjs +57 -0
- package/dist/use.d.ts +260 -0
- package/dist/use.mjs +125 -0
- package/dist/use.test.d.ts +1 -0
- package/dist/use.test.mjs +381 -0
- package/dist/util.d.ts +20 -0
- package/dist/util.mjs +49 -0
- package/dist/util.test.d.ts +4 -0
- package/dist/util.test.mjs +604 -0
- package/dist/vue2.d.ts +15 -0
- package/dist/vue2.mjs +0 -0
- package/dist/vue3.d.ts +13 -0
- package/dist/vue3.mjs +0 -0
- package/package.json +85 -0
- package/src/index.test.ts +273 -0
- package/src/index.ts +15 -0
- package/src/plugin.test.ts +812 -0
- package/src/plugin.ts +107 -0
- package/src/router-link.test.ts +830 -0
- package/src/router-link.ts +172 -0
- package/src/router-view.test.ts +840 -0
- package/src/router-view.ts +59 -0
- package/src/run-with-context.test.ts +64 -0
- package/src/use.test.ts +484 -0
- package/src/use.ts +416 -0
- package/src/util.test.ts +760 -0
- package/src/util.ts +85 -0
- package/src/vue2.ts +18 -0
- 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
|
+
};
|