@esmx/router-vue
Version:
Vue integration for @esmx/router - A universal router that works seamlessly with both Vue 2.7+ and Vue 3
42 lines (41 loc) • 1.28 kB
JavaScript
import { RouterLink } from "./router-link.mjs";
import { RouterView } from "./router-view.mjs";
import { getRoute, getRouter } from "./use.mjs";
import { defineRouterProperties, isVue2 } from "./util.mjs";
export const RouterPlugin = {
/**
* Install the router plugin.
* @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
*/
install(app) {
var _a;
if (!app) {
throw new Error("[@esmx/router-vue] Invalid Vue app instance");
}
const vueApp = app;
const target = ((_a = vueApp.config) == null ? void 0 : _a.globalProperties) || vueApp.prototype;
if (!target) {
throw new Error("[@esmx/router-vue] Invalid Vue app instance");
}
if (isVue2) {
defineRouterProperties(
target,
function() {
return getRouter(this);
},
function() {
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);
}
vueApp.component("RouterLink", RouterLink);
vueApp.component("RouterView", RouterView);
}
};