|
@@ -0,0 +1,52 @@
|
|
|
+/**
|
|
|
+ * 注册子应用钩子
|
|
|
+ * @param render
|
|
|
+ * @param instance
|
|
|
+ * @returns {{unmount: ((function(): Promise<void>)|*), bootstrap: ((function(*): Promise<void>)|*), mount: ((function(*): Promise<void>)|*)}}
|
|
|
+ */
|
|
|
+export function subAppRegister ({ render } = {}) {
|
|
|
+ let instance
|
|
|
+ return {
|
|
|
+ bootstrap: async function (props) {
|
|
|
+ console.log('[sub-app] bootstrap', props)
|
|
|
+ },
|
|
|
+ mount: async function (props) {
|
|
|
+ console.log('[sub-app] mount', props)
|
|
|
+ instance = render(props)
|
|
|
+ },
|
|
|
+ unmount: async function () {
|
|
|
+ console.log('[sub-app] unmount')
|
|
|
+ if (instance) {
|
|
|
+ instance.$destroy()
|
|
|
+ instance.$el.innerHTML = ''
|
|
|
+ instance = null
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 自动判断环境初始化子应用、注册子应用钩子
|
|
|
+ * @param options - vue 实例化配置
|
|
|
+ * @param env - 环境,仅 development 下初始化子应用
|
|
|
+ * @returns {{unmount: ((function(): Promise<void>)|*), bootstrap: ((function(*): Promise<void>)|*), mount: ((function(*): Promise<void>)|*)}}
|
|
|
+ */
|
|
|
+export function easySubAppRegister (options, env = 'development') {
|
|
|
+ const { Vue, router, store, App, el } = options
|
|
|
+ function render (props = {}) {
|
|
|
+ const { container } = props
|
|
|
+ return new Vue({
|
|
|
+ router,
|
|
|
+ store,
|
|
|
+ render: (h) => h(App)
|
|
|
+ }).$mount(container ? container : el)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (window.__POWERED_BY_QIANKUN__) {
|
|
|
+ return subAppRegister({ render: render })
|
|
|
+ } else if (env === 'development') {
|
|
|
+ render()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|