w-vue-middle
Version:
统一公共服务组件
129 lines (108 loc) • 2.97 kB
JavaScript
/*
* @Author: Jason Liu
* @Date: 2023-06-07 17:08:11
* @Desc:
*/
import Vue from "vue";
import Router from "vue-router";
import { AuthenticateUser } from "../utils/auth"; //鉴权
const { i18n } = require("w-vue-basis/interService");
Vue.use(Router);
import "./public-path";
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push;
const originalReplace = Router.prototype.replace;
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err);
};
Router.prototype.replace = function push(location) {
return originalReplace.call(this, location).catch(err => err);
};
/**
* @Author: Jason Liu
* @description: 微前端路由服务
*/
export function microRouter(
option = {
mode: "history"
}
) {
if (window.__POWERED_BY_QIANKUN__) {
option.mode = "abstract";
}
this.router = new Router({
scrollBehavior: () => ({ y: 0 }),
...option
});
return this.router;
}
/**
* @Author: Jason Liu
* @description: 微前端注册服务
*/
export function microRegister(option = { el: "", verified: true }) {
this._option = option;
this.appInstance = undefined;
// window.__POWERED_BY_QIANKUN__可用于判断当前项目是否由qiankun驱动
if (!window.__POWERED_BY_QIANKUN__) {
if (option.verified) {
AuthenticateUser(); //非微前端需要鉴权验证用户信息
}
this.init();
}
}
microRegister.prototype.init = function(props = {}) {
const { container, routerBase, routePath, query, token } = props;
if (props.router) {
window.parentRouter = Vue.prototype.parentRouter = Vue.prototype.$parentRouter =
props.router;
} else {
window.parentRouter = Vue.prototype.parentRouter = Vue.prototype.$parentRouter = this._option.router;
}
this.appInstance = new Vue({
i18n,
...this._option,
el: container ? container.querySelector(this._option.el) : this._option.el
});
this.appInstance.parentRouter = window.parentRouter;
window.$router = this.appInstance.$router;
$winexI18n.initDataFactory();
if (routePath) {
// qiankun loadMicroApp
this.appInstance.$router.push({
path: routePath,
query: query
});
}
};
/**
* @Author: Jason Liu
* @description: bootstrap事件
*/
microRegister.prototype.bootstrap = function(props = {}) {};
/**
* @Author: Jason Liu
* @description: mount事件
*/
microRegister.prototype.mount = function(props = {}) {
this.init(props);
};
/**
* @Author: Jason Liu
* @description: unmount事件
*/
microRegister.prototype.unmount = function(props = {}) {
this.appInstance.$destroy && this.appInstance.$destroy();
};
/**
* @Author: Jason Liu
* @description: update事件
*/
microRegister.prototype.update = function(
props = { routePath: undefined, query: undefined }
) {
this.appInstance.$router.push({
path: props.routePath,
query: props.query
});
};