|
@@ -0,0 +1,103 @@
|
|
|
+import { registerMicroApps, start, initGlobalState, addGlobalUncaughtErrorHandler } from 'qiankun'
|
|
|
+import { openLink, getQKRoute } from '../index'
|
|
|
+
|
|
|
+let action = null
|
|
|
+
|
|
|
+export default {
|
|
|
+ namespaced: true,
|
|
|
+ state: () => ({
|
|
|
+ MicroApps: [],
|
|
|
+ loading: true,
|
|
|
+ abnormal: {
|
|
|
+ status: false,
|
|
|
+ message: ''
|
|
|
+ },
|
|
|
+ sharedModel: {
|
|
|
+ use: 'parent use'
|
|
|
+ }
|
|
|
+ }),
|
|
|
+ mutations: {
|
|
|
+ register (state, data) {
|
|
|
+ state.MicroApps = state.MicroApps.concat(data)
|
|
|
+ },
|
|
|
+ toggleLoading (state, type) {
|
|
|
+ state.loading = type
|
|
|
+ },
|
|
|
+ syncModel (state, data) {
|
|
|
+ state.sharedModel = data
|
|
|
+ },
|
|
|
+ toggleAbnormal (state, { type, message }) {
|
|
|
+ state.abnormal.status = type
|
|
|
+ state.abnormal.message = message
|
|
|
+ }
|
|
|
+ },
|
|
|
+ actions: {
|
|
|
+ async microAppsStart ({ state, getters, commit, dispatch }) {
|
|
|
+ registerMicroApps(getters.apps, {
|
|
|
+ beforeLoad: (app) => {
|
|
|
+ commit('toggleLoading', true)
|
|
|
+ if (app.props.__QIANKUN_ROUTER_BASE) {
|
|
|
+ window.__QIANKUN_ROUTER_BASE = app.props.__QIANKUN_ROUTER_BASE
|
|
|
+ }
|
|
|
+ },
|
|
|
+ afterMount: (app) => {
|
|
|
+ console.log('after mount', app)
|
|
|
+ commit('toggleAbnormal', {
|
|
|
+ type: false
|
|
|
+ })
|
|
|
+ setTimeout(() => {
|
|
|
+ commit('toggleLoading', false)
|
|
|
+ }, 300)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ start({
|
|
|
+ sandbox: {
|
|
|
+ strictStyleIsolation: true
|
|
|
+ }
|
|
|
+ })
|
|
|
+ addGlobalUncaughtErrorHandler((event) => {
|
|
|
+ commit('toggleLoading', false)
|
|
|
+ commit('toggleAbnormal', {
|
|
|
+ type: true,
|
|
|
+ message: '抱歉,加载应用未成功,请稍后重试'
|
|
|
+ })
|
|
|
+ })
|
|
|
+ dispatch('syncModel', state.sharedModel)
|
|
|
+ },
|
|
|
+ syncModel ({ state, commit }, payload = {}) {
|
|
|
+ if (!action) {
|
|
|
+ action = initGlobalState(payload)
|
|
|
+
|
|
|
+ action.onGlobalStateChange((data, prev) => {
|
|
|
+ commit('syncModel', data)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ action.setGlobalState(payload)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getters: {
|
|
|
+ hooks () {
|
|
|
+ return {
|
|
|
+ openLink: openLink
|
|
|
+ }
|
|
|
+ },
|
|
|
+ apps (state, getters) {
|
|
|
+ return state.MicroApps.map(v => {
|
|
|
+ console.log(getQKRoute(v.rule, true), 'xx rr')
|
|
|
+ return Object.assign({}, {
|
|
|
+ name: v.name,
|
|
|
+ entry: v.entry,
|
|
|
+ container: '#app-container',
|
|
|
+ activeRule: getQKRoute(v.rule, true)
|
|
|
+ }, v, {
|
|
|
+ props: Object.assign({}, {
|
|
|
+ __QIANKUN_ROUTER_BASE: getQKRoute(v.rule, true),
|
|
|
+ hooks: getters.hooks
|
|
|
+ }, v.props)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|