@esmx/router-vue
Version:
Vue integration for @esmx/router - A universal router that works seamlessly with both Vue 2.7+ and Vue 3
36 lines (35 loc) • 1.02 kB
JavaScript
import { RouterLink } from "./router-link.mjs";
import { RouterView } from "./router-view.mjs";
import { getRoute, getRouter } from "./use.mjs";
import { isVue3 } 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");
}
Object.defineProperties(target, {
$router: {
get() {
return getRouter(isVue3 ? null : this);
}
},
$route: {
get() {
return getRoute(isVue3 ? null : this);
}
}
});
vueApp.component("RouterLink", RouterLink);
vueApp.component("RouterView", RouterView);
}
};