From 3a646ffbfb8c2dc3bb4dbb6950fa6ce41a951491 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 3 Aug 2021 21:31:17 +0800 Subject: [PATCH] fix(app): handle lifecycle hook error --- .../uni-app-plus/dist/uni-app-service.es.js | 25 +++++++++++-------- packages/uni-core/src/helpers/hook.ts | 12 +++------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/packages/uni-app-plus/dist/uni-app-service.es.js b/packages/uni-app-plus/dist/uni-app-service.es.js index 753a84b17..4e84b5fe7 100644 --- a/packages/uni-app-plus/dist/uni-app-service.es.js +++ b/packages/uni-app-plus/dist/uni-app-service.es.js @@ -1067,6 +1067,13 @@ var serviceContext = (function (vue) { function cacheStringFunction(fn) { return cache(fn); } + const invokeArrayFns = (fns, arg) => { + let ret; + for (let i = 0; i < fns.length; i++) { + ret = fns[i](arg); + } + return ret; + }; function once(fn, ctx = null) { let res; return ((...args) => { @@ -1867,14 +1874,7 @@ var serviceContext = (function (vue) { } } const hooks = vm.$[name]; - if (!hooks) { - return; - } - let ret; - for (let i = 0; i < hooks.length; i++) { - ret = vue.callWithErrorHandling(hooks[i], vm.$, name, [args]); - } - return ret; + return hooks && invokeArrayFns(hooks, args); } function normalizeRoute(toRoute) { @@ -8941,8 +8941,13 @@ var serviceContext = (function (vue) { } }); if (mpType === 'page') { - invokeHook(publicThis, ON_LOAD, instance.attrs.__pageQuery); - invokeHook(publicThis, ON_SHOW); + try { + invokeHook(publicThis, ON_LOAD, instance.attrs.__pageQuery); + invokeHook(publicThis, ON_SHOW); + } + catch (e) { + console.error(e.message + '\n' + e.stack); + } } } diff --git a/packages/uni-core/src/helpers/hook.ts b/packages/uni-core/src/helpers/hook.ts index f430a486a..56c37813e 100644 --- a/packages/uni-core/src/helpers/hook.ts +++ b/packages/uni-core/src/helpers/hook.ts @@ -1,5 +1,6 @@ -import { callWithErrorHandling, ComponentPublicInstance } from 'vue' +import { ComponentPublicInstance } from 'vue' import { isString } from '@vue/shared' +import { invokeArrayFns } from '@dcloudio/uni-shared' import { getCurrentPageVm } from './page' export function invokeHook(name: string, args?: unknown): unknown @@ -36,14 +37,7 @@ export function invokeHook( } } const hooks = vm.$[name as string] - if (!hooks) { - return - } - let ret - for (let i = 0; i < hooks.length; i++) { - ret = callWithErrorHandling(hooks[i], vm.$, name as any, [args]) - } - return ret + return hooks && invokeArrayFns(hooks, args) } export function hasHook(vm: ComponentPublicInstance | number, name: string) { -- GitLab