Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 1x 1x 1x 1x 1x 1x 28x 3x 3x 25x 28x 28x 1x 1x 27x 27x 24x 2x 2x 2x 2x 24x 24x 24x 24x 28x 1x | import { RouterLink } from './router-link';
import { RouterView } from './router-view';
import { getRoute, getRouter } from './use';
import { defineRouterProperties, isVue2 } from './util';
interface VueApp {
config?: {
globalProperties: Record<string, unknown>;
};
prototype?: Record<string, unknown>;
component(name: string, component: unknown): void;
}
/**
* Vue plugin for \@esmx/router integration.
* Registers RouterLink and RouterView components globally.
* Compatible with both Vue 2.7+ and Vue 3.
*
* @example
*
* Vue 3 installation
*
* ```typescript
* import { createApp } from 'vue';
* import { Router } from '@esmx/router';
* import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
*
* const routes = [
* { path: '/', component: Home },
* { path: '/about', component: About }
* ];
*
* const router = new Router({ routes });
* const app = createApp({
* setup() {
* useProvideRouter(router);
* }
* });
*
* app.use(RouterPlugin);
* app.mount('#app');
* ```
*
* @example
*
* Vue 2 installation
*
* ```typescript
* import Vue from 'vue';
* import { Router } from '@esmx/router';
* import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
*
* const routes = [
* { path: '/', component: Home },
* { path: '/about', component: About }
* ];
*
* const router = new Router({ routes });
* Vue.use(RouterPlugin);
*
* new Vue({
* setup() {
* useProvideRouter(router);
* }
* }).$mount('#app');
* ```
*/
export const RouterPlugin = {
/**
* Install the router plugin.
* @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
*/
install(app: unknown): void {
if (!app) {
throw new Error('[@esmx/router-vue] Invalid Vue app instance');
}
const vueApp = app as VueApp;
const target = vueApp.config?.globalProperties || vueApp.prototype;
if (!target) {
throw new Error('[@esmx/router-vue] Invalid Vue app instance');
}
if (isVue2) {
defineRouterProperties(
target,
function (this: any) {
return getRouter(this);
},
function (this: any) {
return getRoute(this);
}
);
} else {
const throwError = () => {
throw new Error(
'[@esmx/router-vue] Router not provided. Please call useProvideRouter() in your root component setup.'
);
};
defineRouterProperties(target, throwError, throwError, true);
}
// Register global components
vueApp.component('RouterLink', RouterLink);
vueApp.component('RouterView', RouterView);
}
};
|