123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /**
- * 生产子应用路由
- * @param subConfig {Array.<Object>}
- * @param subConfig.name 子应用名称需要唯一
- * @param subConfig.url 子应用地址需要访问到html
- * @param subConfig.baseRoute 子应用的publicPath地址
- */
- export const generateSubRoutes = (subConfig = []) => {
- return [].concat(subConfig).map(sub => ({
- path: `${sub.baseRoute.replace(/\/$/, '')}/:subPath(.*)`,
- name: `${sub.name}Sub`,
- props: ({ params }) => ({
- baseRoute: sub.baseRoute,
- name: sub.name,
- url: sub.url,
- subPath: '/' + params.subPath,
- withoutFramework: sub.withoutFramework
- }),
- component: () => import('@cip/components/cip-subapp-container/micro-app')
- }))
- }
- export const microAppRender = (render) => {
- console.time('microAppRender')
- const props = {
- routerBase: window.__MICRO_APP_BASE_ROUTE__,
- ...window.microApp.getData()
- }
- const routerController = (router) => (data) => {
- // 基座传下来的subPath与当前页面的fullPath不一样时才进行转换
- if (router.currentRoute.value.fullPath !== data.subPath) {
- router.push(data.subPath)
- }
- }
- // 异步导致数据返回时间较长
- render(props).then((res) => {
- if (props.subPath) {
- res.router.push(props.subPath).then(() => {
- res.router.afterEach(to => {
- window.microApp.dispatch({ type: 'pathChange', data: to.fullPath })
- })
- })
- }
- window.microApp.addDataListener(routerController(res.router))
- window.addEventListener('unmount', () => {
- // 执行卸载相关操作
- window.microApp.clearDataListener()
- res.instance.unmount()
- res = null
- })
- })
- console.timeEnd('microAppRender')
- }
- export const isSubApp = (name) => /Sub$/.test(name)
- /**
- * 判断是否需要隐藏外出framework,值为字符串正则数组
- * @param withoutFramework {Array<RegExp|String>}
- * @param path {String}
- * @returns {boolean}
- */
- export const judgeHiddenFramework = (withoutFramework, path) => {
- const regxRules = withoutFramework.filter(v => Object.prototype.toString.call(v) === '[object RegExp]')
- if (withoutFramework.includes(path)) return true
- for (let i = 0; i < regxRules.length; i++) {
- const regx = regxRules[i]
- if (regx.test(path)) return true
- }
- return false
- }
|