From 79938ccfad3f985bb2ceeb46d1915de0df86364a Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 31 Oct 2022 15:27:03 +0800 Subject: [PATCH] fix(App): keep onReady before mounted when use composition-api --- packages/uni-app/dist/app.d.ts | 1 + packages/uni-app/dist/app.js | 27 +++++++++++++++++++++++++++ packages/uni-app/dist/index.js | 2 ++ packages/uni-app/src/app.ts | 27 +++++++++++++++++++++++++++ packages/uni-app/src/index.ts | 7 ++++--- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 packages/uni-app/dist/app.d.ts create mode 100644 packages/uni-app/dist/app.js create mode 100644 packages/uni-app/src/app.ts diff --git a/packages/uni-app/dist/app.d.ts b/packages/uni-app/dist/app.d.ts new file mode 100644 index 000000000..41f399821 --- /dev/null +++ b/packages/uni-app/dist/app.d.ts @@ -0,0 +1 @@ +export declare function init(): void; diff --git a/packages/uni-app/dist/app.js b/packages/uni-app/dist/app.js new file mode 100644 index 000000000..b8d2940b4 --- /dev/null +++ b/packages/uni-app/dist/app.js @@ -0,0 +1,27 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.init = void 0; +var Vue = require("vue"); +function init() { + var vueConstructor = (Vue.default ? Vue.default : Vue); + var defaultMergeHook = vueConstructor.config.optionMergeStrategies.mounted; + var onReadyFn; + vueConstructor.config.optionMergeStrategies.mounted = function Le(parentVal, childVal) { + var res = defaultMergeHook.call(this, parentVal, childVal); + if (Array.isArray(res)) { + var index = void 0; + if (onReadyFn) { + index = res.indexOf(onReadyFn); + } + else { + index = res.findIndex(function (fn) { return fn.toString().includes('onReady'); }); + onReadyFn = res[index]; + } + res.splice(index, 1); + res.push(onReadyFn); + } + console.log('Merge mounted:', res); + return res; + }; +} +exports.init = init; diff --git a/packages/uni-app/dist/index.js b/packages/uni-app/dist/index.js index faeef4773..ecfba34ca 100644 --- a/packages/uni-app/dist/index.js +++ b/packages/uni-app/dist/index.js @@ -2,6 +2,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.onNavigationBarSearchInputClicked = exports.onNavigationBarSearchInputConfirmed = exports.onNavigationBarSearchInputChanged = exports.onBackPress = exports.onNavigationBarButtonTap = exports.onTabItemTap = exports.onResize = exports.onPageScroll = exports.onAddToFavorites = exports.onShareTimeline = exports.onShareAppMessage = exports.onReachBottom = exports.onPullDownRefresh = exports.onUnload = exports.onReady = exports.onLoad = exports.onInit = exports.onUniNViewMessage = exports.onThemeChange = exports.onUnhandledRejection = exports.onPageNotFound = exports.onError = exports.onLaunch = exports.onHide = exports.onShow = exports.initUtsPackageName = exports.initUtsClassName = exports.initUtsIndexClassName = exports.initUtsProxyFunction = exports.initUtsProxyClass = void 0; var composition_api_1 = require("@vue/composition-api"); +var app = require("./app"); var mp = require("./mp"); var uts_1 = require("./uts"); Object.defineProperty(exports, "initUtsProxyClass", { enumerable: true, get: function () { return uts_1.initUtsProxyClass; } }); @@ -18,6 +19,7 @@ var createLifeCycle = function (lifecycle) { }; }; if (typeof plus === 'object') { + app.init(); } else if (typeof window === 'object' && 'document' in window) { } diff --git a/packages/uni-app/src/app.ts b/packages/uni-app/src/app.ts new file mode 100644 index 000000000..dd5b42851 --- /dev/null +++ b/packages/uni-app/src/app.ts @@ -0,0 +1,27 @@ +import Vue = require('vue') + +type MergeHook = (parentVal: undefined | Function[], childVal?: Function | Function[]) => void | Function[] + +export function init() { + const vueConstructor = (Vue.default ? Vue.default : Vue) as typeof Vue.default + const defaultMergeHook = vueConstructor.config.optionMergeStrategies.mounted as MergeHook + let onReadyFn: Function + // 向后移动 onReady 触发时机,暂不改动主框架 + vueConstructor.config.optionMergeStrategies.mounted = function Le(parentVal: undefined | Function[], childVal?: Function | Function[]) { + const res = defaultMergeHook.call(this, parentVal, childVal) as ReturnType + if (Array.isArray(res)) { + let index: number + if (onReadyFn) { + index = res.indexOf(onReadyFn) + } else { + index = res.findIndex(fn => fn.toString().includes('onReady')) + onReadyFn = res[index] + } + if (index !== -1) { + res.splice(index, 1) + res.push(onReadyFn) + } + } + return res + } +} diff --git a/packages/uni-app/src/index.ts b/packages/uni-app/src/index.ts index bf51a47ac..7d782d6a0 100644 --- a/packages/uni-app/src/index.ts +++ b/packages/uni-app/src/index.ts @@ -1,14 +1,15 @@ /// import { createLifeCycle as createLifeCycleBase, ComponentInternalInstance } from '@vue/composition-api' +import * as app from './app' import * as mp from './mp' - + export { initUtsProxyClass, initUtsProxyFunction, initUtsIndexClassName, initUtsClassName, initUtsPackageName, -} from './uts' +} from './uts' const lifecycles: string[] = [] @@ -20,7 +21,7 @@ const createLifeCycle = (lifecycle: string) => { } } if (typeof plus === 'object') { - // TODO App + app.init() } else if (typeof window === 'object' && 'document' in window) { // TODO H5 } else { -- GitLab