提交 e135ea3d 编写于 作者: fxy060608's avatar fxy060608

fix(h5): lifecycle

上级 ce1aeb57
......@@ -17305,17 +17305,15 @@ var serviceContext = (function (vue) {
});
if (mpType === 'page') {
instance.__isVisible = true;
// 直接触发页面 onLoad、onShow 组件内的 onLoad 和 onShow 在注册时,直接触发一次
try {
invokeHook(publicThis, ON_LOAD, instance.attrs.__pageQuery);
delete instance.attrs.__pageQuery;
invokeHook(publicThis, ON_SHOW);
}
catch (e) {
console.error(e.message + LINEFEED + e.stack);
}
vue.nextTick(() => {
// 延迟onShow,保证组件的onShow也可以监听到
invokeHook(publicThis, ON_SHOW);
});
}
}
......
......@@ -3844,7 +3844,14 @@ function injectHook(type, hook, target = currentInstance, prepend = false) {
if (target.type.__reserved) {
return;
}
target = target.root;
if (target !== target.root) {
target = target.root;
if (uniShared.isRootImmediateHook(type)) {
// 作用域应该是组件还是页面?目前绑定的是页面
const proxy = target.proxy;
callWithAsyncErrorHandling(hook.bind(proxy), target, type, uniShared.ON_LOAD === type ? [proxy.$page.options] : []);
}
}
}
const hooks = target[type] || (target[type] = []);
// cache the error handling wrapper for injected hooks so the same hook
......
import { extend, isArray, isMap, isIntegerKey, isSymbol, hasOwn, isObject, hasChanged, makeMap, capitalize, toRawType, def, isFunction, NOOP, isString, isPromise, getGlobalThis, EMPTY_OBJ, toHandlerKey, toNumber, hyphenate, camelize, isOn, isModelListener, remove, isSet, isPlainObject, invokeArrayFns, isReservedProp, EMPTY_ARR, isBuiltInDirective, NO, normalizeClass, normalizeStyle, isGloballyWhitelisted, isSpecialBooleanAttr, includeBooleanAttr, looseIndexOf, looseEqual, isHTMLTag, isSVGTag } from '@vue/shared';
export { camelize, capitalize, normalizeClass, normalizeProps, normalizeStyle, toDisplayString, toHandlerKey } from '@vue/shared';
import { isRootHook } from '@dcloudio/uni-shared';
import { isRootHook, isRootImmediateHook, ON_LOAD } from '@dcloudio/uni-shared';
function warn(msg, ...args) {
console.warn(`[Vue warn] ${msg}`, ...args);
......@@ -3869,7 +3869,14 @@ function injectHook(type, hook, target = currentInstance, prepend = false) {
if (target.type.__reserved) {
return;
}
target = target.root;
if (target !== target.root) {
target = target.root;
if (isRootImmediateHook(type)) {
// 作用域应该是组件还是页面?目前绑定的是页面
const proxy = target.proxy;
callWithAsyncErrorHandling(hook.bind(proxy), target, type, ON_LOAD === type ? [proxy.$page.options] : []);
}
}
}
const hooks = target[type] || (target[type] = []);
// cache the error handling wrapper for injected hooks so the same hook
......
......@@ -4306,7 +4306,7 @@ var index$t = /* @__PURE__ */ defineBuiltInComponent({
slots
}) {
const vm = vue.getCurrentInstance();
const __scopeId = vm && vm.root.type.__scopeId || "";
const __scopeId = vm && vm.vnode.scopeId || "";
const {
hovering,
binding
......@@ -5151,7 +5151,7 @@ var index$p = /* @__PURE__ */ defineBuiltInComponent({
if (typeof nodes === "string") {
nodes = parseHtml(nodes);
}
const nodeList = parseNodes(nodes, document.createDocumentFragment(), (vm && vm.root.type).__scopeId || "", hasItemClick && triggerItemClick);
const nodeList = parseNodes(nodes, document.createDocumentFragment(), vm && vm.vnode.scopeId || "", hasItemClick && triggerItemClick);
rootRef.value.firstElementChild.innerHTML = "";
rootRef.value.firstElementChild.appendChild(nodeList);
}
......@@ -6579,12 +6579,10 @@ function initHooks(options, instance, publicThis) {
try {
invokeHook(publicThis, uniShared.ON_LOAD, instance.attrs.__pageQuery);
delete instance.attrs.__pageQuery;
invokeHook(publicThis, uniShared.ON_SHOW);
} catch (e2) {
console.error(e2.message + uniShared.LINEFEED + e2.stack);
}
vue.nextTick(() => {
invokeHook(publicThis, uniShared.ON_SHOW);
});
}
}
function applyOptions(options, instance, publicThis) {
......@@ -6977,9 +6975,11 @@ function setupPage(comp) {
setup(instance) {
instance.root = instance;
const route = usePageRoute();
instance.attrs.__pageQuery = uniShared.decodedQuery(route.query);
const query = uniShared.decodedQuery(route.query);
instance.attrs.__pageQuery = query;
instance.proxy.$page.options = query;
{
return instance.attrs.__pageQuery;
return query;
}
}
});
......
......@@ -10177,7 +10177,7 @@ var index$q = /* @__PURE__ */ defineBuiltInComponent({
slots
}) {
const vm = getCurrentInstance();
const __scopeId = vm && vm.root.type.__scopeId || "";
const __scopeId = vm && vm.vnode.scopeId || "";
const {
hovering,
binding
......@@ -11812,7 +11812,7 @@ var index$m = /* @__PURE__ */ defineBuiltInComponent({
if (typeof nodes === "string") {
nodes = parseHtml(nodes);
}
const nodeList = parseNodes(nodes, document.createDocumentFragment(), (vm && vm.root.type).__scopeId || "", hasItemClick && triggerItemClick);
const nodeList = parseNodes(nodes, document.createDocumentFragment(), vm && vm.vnode.scopeId || "", hasItemClick && triggerItemClick);
rootRef.value.firstElementChild.innerHTML = "";
rootRef.value.firstElementChild.appendChild(nodeList);
}
......@@ -13605,12 +13605,10 @@ function initHooks(options, instance2, publicThis) {
try {
invokeHook(publicThis, ON_LOAD, instance2.attrs.__pageQuery);
delete instance2.attrs.__pageQuery;
invokeHook(publicThis, ON_SHOW);
} catch (e2) {
console.error(e2.message + LINEFEED + e2.stack);
}
nextTick(() => {
invokeHook(publicThis, ON_SHOW);
});
}
}
function applyOptions(options, instance2, publicThis) {
......@@ -14174,7 +14172,9 @@ function setupPage(comp) {
setup(instance2) {
instance2.root = instance2;
const route = usePageRoute();
instance2.attrs.__pageQuery = decodedQuery(route.query);
const query = decodedQuery(route.query);
instance2.attrs.__pageQuery = query;
instance2.proxy.$page.options = query;
const pageMeta = usePageMeta();
onBeforeMount(() => {
onPageShow(instance2, pageMeta);
......@@ -14203,7 +14203,7 @@ function setupPage(comp) {
onBeforeUnmount(() => {
unsubscribeViewMethod(pageMeta.id);
});
return route.query;
return query;
}
});
}
......
......@@ -88,9 +88,11 @@ export function setupPage(comp: any) {
instance.root = instance // 组件 root 指向页面
const route = usePageRoute()
// 存储参数,让 initHooks 中执行 onLoad 时,可以访问到
instance.attrs.__pageQuery = decodedQuery(route.query)
const query = decodedQuery(route.query)
instance.attrs.__pageQuery = query
instance.proxy!.$page.options = query
if (__NODE_JS__) {
return instance.attrs.__pageQuery as Record<string, unknown>
return query
}
const pageMeta = usePageMeta()
onBeforeMount(() => {
......@@ -122,7 +124,7 @@ export function setupPage(comp: any) {
unsubscribeViewMethod(pageMeta.id!)
})
return route.query
return query
},
})
}
......
......@@ -1273,6 +1273,10 @@ const PAGE_HOOKS = [
ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED,
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
];
const PAGE_SYNC_HOOKS = [ON_LOAD, ON_SHOW];
function isRootImmediateHook(name) {
return PAGE_SYNC_HOOKS.indexOf(name) > -1;
}
function isRootHook(name) {
return PAGE_HOOKS.indexOf(name) > -1;
}
......@@ -1520,6 +1524,7 @@ exports.isH5CustomElement = isH5CustomElement;
exports.isH5NativeTag = isH5NativeTag;
exports.isMiniProgramNativeTag = isMiniProgramNativeTag;
exports.isRootHook = isRootHook;
exports.isRootImmediateHook = isRootImmediateHook;
exports.normalizeDataset = normalizeDataset;
exports.normalizeEventType = normalizeEventType;
exports.normalizeTarget = normalizeTarget;
......
......@@ -272,6 +272,8 @@ export declare function isMiniProgramNativeTag(tag: string): boolean;
export declare function isRootHook(name: string): boolean;
export declare function isRootImmediateHook(name: string): boolean;
export declare interface IUniPageNode {
pageId: number;
pageNode: IUniPageNode | null;
......
......@@ -1269,6 +1269,10 @@ const PAGE_HOOKS = [
ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED,
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
];
const PAGE_SYNC_HOOKS = [ON_LOAD, ON_SHOW];
function isRootImmediateHook(name) {
return PAGE_SYNC_HOOKS.indexOf(name) > -1;
}
function isRootHook(name) {
return PAGE_HOOKS.indexOf(name) > -1;
}
......@@ -1380,4 +1384,4 @@ function getEnvLocale() {
return (lang && lang.replace(/[.:].*/, '')) || 'en';
}
export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, BUILT_IN_TAG_NAMES, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, E$1 as Emitter, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, NVUE_BUILT_IN_TAGS, NVUE_U_BUILT_IN_TAGS, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, SLOT_DEFAULT_NAME, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UNI_STORAGE_LOCALE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, addLeadingSlash, cache, cacheStringFunction, callOptions, createIsCustomElement, createRpx2Unit, createUniEvent, customizeEvent, debounce, decode, decodedQuery, defaultMiniProgramRpx2Unit, defaultNVueRpx2Unit, defaultRpx2Unit, dynamicSlotName, forcePatchProp, formatAppLog, formatDateTime, formatH5Log, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDatasetOnce, invokeArrayFns, invokeCreateVueAppHook, isAppNVueNativeTag, isAppNativeTag, isBuiltInComponent, isComponentInternalInstance, isComponentTag, isH5CustomElement, isH5NativeTag, isMiniProgramNativeTag, isRootHook, normalizeDataset, normalizeEventType, normalizeTarget, onCreateVueApp, once, parseEventName, parseNVueDataset, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveComponentInstance, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle };
export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, BUILT_IN_TAG_NAMES, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, E$1 as Emitter, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, NVUE_BUILT_IN_TAGS, NVUE_U_BUILT_IN_TAGS, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, SLOT_DEFAULT_NAME, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UNI_STORAGE_LOCALE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, addLeadingSlash, cache, cacheStringFunction, callOptions, createIsCustomElement, createRpx2Unit, createUniEvent, customizeEvent, debounce, decode, decodedQuery, defaultMiniProgramRpx2Unit, defaultNVueRpx2Unit, defaultRpx2Unit, dynamicSlotName, forcePatchProp, formatAppLog, formatDateTime, formatH5Log, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDatasetOnce, invokeArrayFns, invokeCreateVueAppHook, isAppNVueNativeTag, isAppNativeTag, isBuiltInComponent, isComponentInternalInstance, isComponentTag, isH5CustomElement, isH5NativeTag, isMiniProgramNativeTag, isRootHook, isRootImmediateHook, normalizeDataset, normalizeEventType, normalizeTarget, onCreateVueApp, once, parseEventName, parseNVueDataset, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveComponentInstance, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle };
......@@ -45,6 +45,12 @@ const PAGE_HOOKS = [
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
]
const PAGE_SYNC_HOOKS = [ON_LOAD, ON_SHOW]
export function isRootImmediateHook(name: string) {
return PAGE_SYNC_HOOKS.indexOf(name) > -1
}
export function isRootHook(name: string) {
return PAGE_HOOKS.indexOf(name) > -1
}
......
......@@ -3,7 +3,6 @@ import { LINEFEED, ON_LOAD, ON_SHOW } from '@dcloudio/uni-shared'
import { isArray, isFunction } from '@vue/shared'
import {
nextTick,
ComponentOptions,
ComponentInternalInstance,
ComponentPublicInstance,
......@@ -46,15 +45,13 @@ export function initHooks(
})
if ((__PLATFORM__ === 'app' || __PLATFORM__ === 'h5') && mpType === 'page') {
instance.__isVisible = true
// 直接触发页面 onLoad、onShow 组件内的 onLoad 和 onShow 在注册时,直接触发一次
try {
invokeHook(publicThis, ON_LOAD, instance.attrs.__pageQuery)
delete instance.attrs.__pageQuery
invokeHook(publicThis, ON_SHOW)
} catch (e: any) {
console.error(e.message + LINEFEED + e.stack)
}
nextTick(() => {
// 延迟onShow,保证组件的onShow也可以监听到
invokeHook(publicThis, ON_SHOW)
})
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册