Skip to content

Commit af3a851

Browse files
committed
fix(uni-mp-xhs): 小红书 Page 生命周期调整
1 parent 245702a commit af3a851

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

packages/uni-mp-xhs/dist/uni.mp.esm.js

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SLOT_DEFAULT_NAME, EventChannel, invokeArrayFns, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, ON_LOAD, ON_SHOW, ON_HIDE, ON_UNLOAD, ON_RESIZE, ON_TAB_ITEM_TAP, ON_REACH_BOTTOM, ON_PULL_DOWN_REFRESH, ON_ADD_TO_FAVORITES, isUniLifecycleHook, ON_READY, ON_LAUNCH, ON_ERROR, ON_THEME_CHANGE, ON_PAGE_NOT_FOUND, ON_UNHANDLE_REJECTION, customizeEvent, addLeadingSlash, stringifyQuery } from '@dcloudio/uni-shared';
2-
import { isArray, isFunction, hasOwn, extend, hyphenate, isPlainObject, isObject } from '@vue/shared';
2+
import { isArray, hasOwn, isFunction, extend, hyphenate, isPlainObject, isObject } from '@vue/shared';
33
import { ref, nextTick, findComponentPropsData, toRaw, updateProps, hasQueueJob, invalidateJob, devtoolsComponentAdded, getExposeProxy, pruneComponentPropsCache } from 'vue';
44
import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n';
55

@@ -840,22 +840,6 @@ function initSpecialMethods(mpInstance) {
840840
});
841841
}
842842
}
843-
function createVueComponent(mpType, mpInstance, vueOptions, parent) {
844-
return $createComponent({
845-
type: vueOptions,
846-
props: findPropsData(mpInstance.props, mpType === 'page'),
847-
}, {
848-
mpType,
849-
mpInstance,
850-
slots: mpInstance.props.uS || {}, // vueSlots
851-
parentComponent: parent && parent.$,
852-
onBeforeSetup(instance, options) {
853-
initRefs(instance, mpInstance);
854-
initMocks(instance, mpInstance, mocks$1);
855-
initComponentInstance(instance, options);
856-
},
857-
});
858-
}
859843

860844
function initCreatePage() {
861845
return function createPage(vueOptions) {
@@ -868,18 +852,30 @@ function initCreatePage() {
868852
};
869853
// 初始化 vue 实例
870854
this.props = query;
871-
this.$vm = createVueComponent('page', this, vueOptions);
855+
const mpInstance = this;
856+
this.$vm = $createComponent({
857+
type: vueOptions,
858+
props: findPropsData(this.props, true),
859+
}, {
860+
mpType: 'page',
861+
mpInstance: this,
862+
slots: this.props.uS || {}, // vueSlots
863+
onBeforeSetup(instance, options) {
864+
initRefs(instance, mpInstance);
865+
initMocks(instance, mpInstance, mocks$1);
866+
initComponentInstance(instance, options);
867+
},
868+
});
872869
initSpecialMethods(this);
873-
this.$vm.$callHook(ON_LOAD, this.options);
870+
this.$vm.$callHook(ON_LOAD, query);
874871
},
875872
onShow() {
876-
this.$vm.$callHook(ON_SHOW);
877873
if (__VUE_PROD_DEVTOOLS__) {
878874
devtoolsComponentAdded(this.$vm.$);
879875
}
876+
this.$vm.$callHook(ON_SHOW);
880877
},
881878
onReady() {
882-
// 确保页面自定义组件都被收集到
883879
setTimeout(() => {
884880
this.$vm.$callHook('mounted');
885881
this.$vm.$callHook(ON_READY);

packages/uni-mp-xhs/src/runtime/createPage.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import {
2+
type ComponentInternalInstance,
23
type ComponentOptions,
3-
// @ts-expect-error
4+
// @ts-expect-error: 模块“"vue"”没有导出的成员“devtoolsComponentAdded”
45
devtoolsComponentAdded,
56
} from 'vue'
67

78
import {
9+
$createComponent,
810
$destroyComponent,
11+
type CreateComponentOptions,
912
PAGE_INIT_HOOKS,
13+
findPropsData,
1014
handleEvent,
15+
initComponentInstance,
1116
initData,
1217
initHooks,
18+
initMocks,
19+
initRefs,
1320
initRuntimeHooks,
1421
initUnknownHooks,
1522
initWxsCallMethods,
@@ -24,7 +31,7 @@ import {
2431
stringifyQuery,
2532
} from '@dcloudio/uni-shared'
2633

27-
import { createVueComponent, handleLink, initSpecialMethods } from './util'
34+
import { handleLink, initSpecialMethods, mocks } from './util'
2835

2936
import { extend, isPlainObject } from '@vue/shared'
3037

@@ -41,18 +48,36 @@ export function initCreatePage() {
4148
}
4249
// 初始化 vue 实例
4350
this.props = query
44-
this.$vm = createVueComponent('page', this, vueOptions)
51+
const mpInstance = this
52+
this.$vm = $createComponent(
53+
{
54+
type: vueOptions,
55+
props: findPropsData(this.props, true),
56+
},
57+
{
58+
mpType: 'page',
59+
mpInstance: this,
60+
slots: this.props.uS || {}, // vueSlots
61+
onBeforeSetup(
62+
instance: ComponentInternalInstance,
63+
options: CreateComponentOptions
64+
) {
65+
initRefs(instance, mpInstance as any)
66+
initMocks(instance, mpInstance as any, mocks)
67+
initComponentInstance(instance, options)
68+
},
69+
}
70+
)
4571
initSpecialMethods(this)
46-
this.$vm.$callHook(ON_LOAD, this.options)
72+
this.$vm.$callHook(ON_LOAD, query)
4773
},
4874
onShow() {
49-
this.$vm.$callHook(ON_SHOW)
5075
if (__VUE_PROD_DEVTOOLS__) {
5176
devtoolsComponentAdded(this.$vm.$)
5277
}
78+
this.$vm.$callHook(ON_SHOW)
5379
},
5480
onReady() {
55-
// 确保页面自定义组件都被收集到
5681
setTimeout(() => {
5782
this.$vm.$callHook('mounted')
5883
this.$vm.$callHook(ON_READY)

0 commit comments

Comments
 (0)