|
@@ -1,25 +1,43 @@
|
|
|
/**
|
|
|
- * 尝试函数调用
|
|
|
- * @param hooks
|
|
|
+ * tryCatch Fn
|
|
|
* @param fn
|
|
|
- * @param arg
|
|
|
- * @param spareFn - 备用函数
|
|
|
+ * @param spareFn
|
|
|
+ * @param args
|
|
|
+ * @returns {*}
|
|
|
+ */
|
|
|
+export function tryCallHooks ({ fn, spareFn = () => null }, ...args) {
|
|
|
+ let result
|
|
|
+ try {
|
|
|
+ result = fn.apply(void 0, args)
|
|
|
+ } catch (e) {
|
|
|
+ result = spareFn.apply(void 0, args)
|
|
|
+ }
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 尝试函数调用
|
|
|
+ * @param options
|
|
|
+ * @param options.fSet - 函数集合
|
|
|
+ * @param options.fKey - 需要调用的函数名
|
|
|
+ * @param [options.spareFn] - 备用函数
|
|
|
+ * @param [args] - 函数 params
|
|
|
* @returns {*}
|
|
|
*/
|
|
|
-export function tryCallHooks (hooks, fn, arg, spareFn = () => null) {
|
|
|
+export function tryCallFunctions ({ fSet, fKey, spareFn = () => null }, ...args) {
|
|
|
try {
|
|
|
- if (!hooks) {
|
|
|
- return spareFn(arg)
|
|
|
+ if (!fSet) {
|
|
|
+ return spareFn.apply(void 0, args)
|
|
|
}
|
|
|
- const callFn = hooks[fn]
|
|
|
+ const callFn = fSet[fKey]
|
|
|
if (typeof callFn === 'function') {
|
|
|
- return callFn(arg)
|
|
|
+ return callFn.apply(void 0, args)
|
|
|
} else {
|
|
|
- spareFn(arg)
|
|
|
+ return spareFn.apply(void 0, args)
|
|
|
}
|
|
|
} catch (e) {
|
|
|
console.warn(e)
|
|
|
- spareFn(arg)
|
|
|
+ return spareFn.apply(void 0, args)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -33,19 +51,28 @@ export function subAppRegister ({ render, Lifecycle = {} } = {}) {
|
|
|
let instance
|
|
|
return Object.assign({}, Lifecycle, {
|
|
|
bootstrap: async function (props) {
|
|
|
- tryCallHooks(Lifecycle, 'bootstrap', props)
|
|
|
+ tryCallFunctions({
|
|
|
+ fSet: Lifecycle,
|
|
|
+ fKey: 'bootstrap'
|
|
|
+ }, props)
|
|
|
},
|
|
|
mount: async function (props) {
|
|
|
+ tryCallFunctions({
|
|
|
+ fSet: Lifecycle,
|
|
|
+ fKey: 'mount'
|
|
|
+ }, props)
|
|
|
instance = render(props)
|
|
|
- tryCallHooks(Lifecycle, 'mount', props)
|
|
|
},
|
|
|
unmount: async function () {
|
|
|
+ tryCallFunctions({
|
|
|
+ fSet: Lifecycle,
|
|
|
+ fKey: 'unmount'
|
|
|
+ })
|
|
|
if (instance) {
|
|
|
instance.$destroy()
|
|
|
instance.$el.innerHTML = ''
|
|
|
instance = null
|
|
|
}
|
|
|
- tryCallHooks(Lifecycle, 'unmount')
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -58,13 +85,17 @@ export function subAppRegister ({ render, Lifecycle = {} } = {}) {
|
|
|
*/
|
|
|
export function easySubAppRegister (options, Lifecycle = {}, env = 'development') {
|
|
|
function render (props = {}) {
|
|
|
- const { Vue, router, store, App, el, container, hooks = {}, className = 'in-app' } = Object.assign({}, options, props)
|
|
|
- Vue.prototype.$BraceHooks = hooks
|
|
|
+ const { Vue, router, store, App, el, container, className = 'in-app', inject = () => {} } = Object.assign({}, options, props)
|
|
|
const app = new Vue({
|
|
|
router,
|
|
|
store,
|
|
|
render: (h) => h(App)
|
|
|
})
|
|
|
+ inject({
|
|
|
+ Vue,
|
|
|
+ router,
|
|
|
+ store
|
|
|
+ })
|
|
|
app.$mount(container ? container.querySelector('#app') : el)
|
|
|
if (container) {
|
|
|
app.$el.classList.add(className)
|
|
@@ -73,7 +104,7 @@ export function easySubAppRegister (options, Lifecycle = {}, env = 'development'
|
|
|
}
|
|
|
|
|
|
if (window.__POWERED_BY_QIANKUN__) {
|
|
|
- return subAppRegister({ render: render, Lifecycle })
|
|
|
+ return subAppRegister({ render: render, Lifecycle, options })
|
|
|
} else if (env === 'development') {
|
|
|
render()
|
|
|
return {}
|