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

chore(mp): build

上级 16f7cbd1
...@@ -122,19 +122,56 @@ function initMocks(instance, mpInstance, mocks) { ...@@ -122,19 +122,56 @@ function initMocks(instance, mpInstance, mocks) {
}); });
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_BACK_PRESS = 'onBackPress';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [ const PAGE_HOOKS = [
'onLoad', ON_LOAD,
'onShow', ON_SHOW,
// 'onReady', // lifetimes.ready ON_HIDE,
'onHide', ON_UNLOAD,
'onUnload', ON_RESIZE,
'onResize', ON_TAB_ITEM_TAP,
// 'onPageScroll', // 影响性能,开发者手动注册 ON_REACH_BOTTOM,
'onTabItemTap', ON_PULL_DOWN_REFRESH,
'onReachBottom', ON_ADD_TO_FAVORITES,
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites',
]; ];
function findHooks(vueOptions, hooks = new Set()) { function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) { if (vueOptions) {
...@@ -162,7 +199,7 @@ function initHook(mpOptions, hook, excludes) { ...@@ -162,7 +199,7 @@ function initHook(mpOptions, hook, excludes) {
}; };
} }
} }
const EXCLUDE_HOOKS = ['onReady']; const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach((hook) => initHook(mpOptions, hook, excludes)); hooks.forEach((hook) => initHook(mpOptions, hook, excludes));
} }
...@@ -171,12 +208,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { ...@@ -171,12 +208,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
} }
const HOOKS = [ const HOOKS = [
'onShow', ON_SHOW,
'onHide', ON_HIDE,
'onError', ON_ERROR,
'onThemeChange', ON_THEME_CHANGE,
'onPageNotFound', ON_PAGE_NOT_FOUND,
'onUnhandledRejection', ON_UNHANDLE_REJECTION,
]; ];
function parseApp(instance, parseAppOptions) { function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$; const internalInstance = instance.$;
...@@ -195,7 +232,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -195,7 +232,7 @@ function parseApp(instance, parseAppOptions) {
slots: [], slots: [],
}); });
ctx.globalData = this.globalData; ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options); instance.$callHook(ON_LAUNCH, options);
}, },
}; };
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
...@@ -216,26 +253,6 @@ function initCreateApp(parseAppOptions) { ...@@ -216,26 +253,6 @@ function initCreateApp(parseAppOptions) {
}; };
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initVueIds(vueIds, mpInstance) { function initVueIds(vueIds, mpInstance) {
if (!vueIds) { if (!vueIds) {
return; return;
...@@ -870,7 +887,7 @@ function initChildVues(mpInstance) { ...@@ -870,7 +887,7 @@ function initChildVues(mpInstance) {
} }
initChildVues(childMPInstance); initChildVues(childMPInstance);
childMPInstance.$vm.$callHook('mounted'); childMPInstance.$vm.$callHook('mounted');
childMPInstance.$vm.$callHook('onReady'); childMPInstance.$vm.$callHook(ON_READY);
}); });
} }
delete mpInstance._$childVues; delete mpInstance._$childVues;
...@@ -977,23 +994,23 @@ function createPage(vueOptions) { ...@@ -977,23 +994,23 @@ function createPage(vueOptions) {
// 初始化 vue 实例 // 初始化 vue 实例
this.$vm = createVueComponent('page', this, vueOptions); this.$vm = createVueComponent('page', this, vueOptions);
initSpecialMethods(this); initSpecialMethods(this);
this.$vm.$callHook('onLoad', query); this.$vm.$callHook(ON_LOAD, query);
}, },
onReady() { onReady() {
initChildVues(this); initChildVues(this);
this.$vm.$callHook('mounted'); this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady'); this.$vm.$callHook(ON_READY);
}, },
onUnload() { onUnload() {
if (this.$vm) { if (this.$vm) {
this.$vm.$callHook('onUnload'); this.$vm.$callHook(ON_UNLOAD);
$destroyComponent(this.$vm); $destroyComponent(this.$vm);
} }
}, },
events: { events: {
// 支付宝小程序有些页面事件只能放在events下 // 支付宝小程序有些页面事件只能放在events下
onBack() { onBack() {
this.$vm.$callHook('onBackPress'); this.$vm.$callHook(ON_BACK_PRESS);
}, },
}, },
__r: handleRef, __r: handleRef,
......
...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) { ...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) {
}); });
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [ const PAGE_HOOKS = [
'onLoad', ON_LOAD,
'onShow', ON_SHOW,
// 'onReady', // lifetimes.ready ON_HIDE,
'onHide', ON_UNLOAD,
'onUnload', ON_RESIZE,
'onResize', ON_TAB_ITEM_TAP,
// 'onPageScroll', // 影响性能,开发者手动注册 ON_REACH_BOTTOM,
'onTabItemTap', ON_PULL_DOWN_REFRESH,
'onReachBottom', ON_ADD_TO_FAVORITES,
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites',
]; ];
function findHooks(vueOptions, hooks = new Set()) { function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) { if (vueOptions) {
...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) { ...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) {
}; };
} }
} }
const EXCLUDE_HOOKS = ['onReady']; const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes)); hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
} }
...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { ...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
} }
const HOOKS = [ const HOOKS = [
'onShow', ON_SHOW,
'onHide', ON_HIDE,
'onError', ON_ERROR,
'onThemeChange', ON_THEME_CHANGE,
'onPageNotFound', ON_PAGE_NOT_FOUND,
'onUnhandledRejection', ON_UNHANDLE_REJECTION,
]; ];
function parseApp(instance, parseAppOptions) { function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$; const internalInstance = instance.$;
...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) {
slots: [], slots: [],
}); });
ctx.globalData = this.globalData; ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options); instance.$callHook(ON_LAUNCH, options);
}, },
}; };
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) { ...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) {
}; };
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) { function initBehavior(options) {
return Behavior(options); return Behavior(options);
} }
...@@ -803,7 +819,7 @@ function parsePage(vueOptions, parseOptions) { ...@@ -803,7 +819,7 @@ function parsePage(vueOptions, parseOptions) {
this.$page = { this.$page = {
fullPath: '/' + this.route + stringifyQuery(query), fullPath: '/' + this.route + stringifyQuery(query),
}; };
return this.$vm && this.$vm.$callHook('onLoad', query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
...@@ -843,7 +859,7 @@ function initHook(name, options) { ...@@ -843,7 +859,7 @@ function initHook(name, options) {
} }
} }
Page = function (options) { Page = function (options) {
initHook('onLoad', options); initHook(ON_LOAD, options);
return MPPage(options); return MPPage(options);
}; };
Component = function (options) { Component = function (options) {
...@@ -857,7 +873,7 @@ function parse$2(appOptions) { ...@@ -857,7 +873,7 @@ function parse$2(appOptions) {
if (!this.$vm) { if (!this.$vm) {
this.onLaunch(args); this.onLaunch(args);
} }
this.$vm.$callHook('onShow', args); this.$vm.$callHook(ON_SHOW, args);
}; };
} }
...@@ -898,7 +914,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { ...@@ -898,7 +914,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800 // https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) { if (this.$vm) {
this.$vm.$callHook('mounted'); this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady'); this.$vm.$callHook(ON_READY);
} }
}, },
detached() { detached() {
...@@ -945,8 +961,8 @@ function parse$1(componentOptions) { ...@@ -945,8 +961,8 @@ function parse$1(componentOptions) {
const pageInstance = this.pageinstance; const pageInstance = this.pageinstance;
pageInstance.$vm = this.$vm; pageInstance.$vm = this.$vm;
if (hasOwn(pageInstance, '_$args')) { if (hasOwn(pageInstance, '_$args')) {
this.$vm.$callHook('onLoad', pageInstance._$args); this.$vm.$callHook(ON_LOAD, pageInstance._$args);
this.$vm.$callHook('onShow'); this.$vm.$callHook(ON_SHOW);
delete pageInstance._$args; delete pageInstance._$args;
} }
} }
...@@ -983,15 +999,15 @@ function parse(pageOptions) { ...@@ -983,15 +999,15 @@ function parse(pageOptions) {
// 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题 // 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
methods.onShow = function onShow() { methods.onShow = function onShow() {
if (this.$vm && this._$loaded) { if (this.$vm && this._$loaded) {
this.$vm.$callHook('onShow'); this.$vm.$callHook(ON_SHOW);
} }
}; };
methods.onLoad = function onLoad(args) { methods.onLoad = function onLoad(args) {
// 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad // 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad
if (this.$vm) { if (this.$vm) {
this._$loaded = true; this._$loaded = true;
this.$vm.$callHook('onLoad', args); this.$vm.$callHook(ON_LOAD, args);
this.$vm.$callHook('onShow'); this.$vm.$callHook(ON_SHOW);
} }
else { else {
this.pageinstance._$args = args; this.pageinstance._$args = args;
......
...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) { ...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) {
}); });
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [ const PAGE_HOOKS = [
'onLoad', ON_LOAD,
'onShow', ON_SHOW,
// 'onReady', // lifetimes.ready ON_HIDE,
'onHide', ON_UNLOAD,
'onUnload', ON_RESIZE,
'onResize', ON_TAB_ITEM_TAP,
// 'onPageScroll', // 影响性能,开发者手动注册 ON_REACH_BOTTOM,
'onTabItemTap', ON_PULL_DOWN_REFRESH,
'onReachBottom', ON_ADD_TO_FAVORITES,
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites',
]; ];
function findHooks(vueOptions, hooks = new Set()) { function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) { if (vueOptions) {
...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) { ...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) {
}; };
} }
} }
const EXCLUDE_HOOKS = ['onReady']; const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes)); hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
} }
...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { ...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
} }
const HOOKS = [ const HOOKS = [
'onShow', ON_SHOW,
'onHide', ON_HIDE,
'onError', ON_ERROR,
'onThemeChange', ON_THEME_CHANGE,
'onPageNotFound', ON_PAGE_NOT_FOUND,
'onUnhandledRejection', ON_UNHANDLE_REJECTION,
]; ];
function parseApp(instance, parseAppOptions) { function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$; const internalInstance = instance.$;
...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) {
slots: [], slots: [],
}); });
ctx.globalData = this.globalData; ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options); instance.$callHook(ON_LAUNCH, options);
}, },
}; };
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) { ...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) {
}; };
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) { function initBehavior(options) {
return Behavior(options); return Behavior(options);
} }
...@@ -784,7 +800,7 @@ function parsePage(vueOptions, parseOptions) { ...@@ -784,7 +800,7 @@ function parsePage(vueOptions, parseOptions) {
this.$page = { this.$page = {
fullPath: '/' + this.route + stringifyQuery(query), fullPath: '/' + this.route + stringifyQuery(query),
}; };
return this.$vm && this.$vm.$callHook('onLoad', query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
...@@ -824,7 +840,7 @@ function initHook(name, options) { ...@@ -824,7 +840,7 @@ function initHook(name, options) {
} }
} }
Page = function (options) { Page = function (options) {
initHook('onLoad', options); initHook(ON_LOAD, options);
return MPPage(options); return MPPage(options);
}; };
Component = function (options) { Component = function (options) {
...@@ -864,7 +880,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { ...@@ -864,7 +880,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800 // https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) { if (this.$vm) {
this.$vm.$callHook('mounted'); this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady'); this.$vm.$callHook(ON_READY);
} }
}, },
detached() { detached() {
......
...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) { ...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) {
}); });
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [ const PAGE_HOOKS = [
'onLoad', ON_LOAD,
'onShow', ON_SHOW,
// 'onReady', // lifetimes.ready ON_HIDE,
'onHide', ON_UNLOAD,
'onUnload', ON_RESIZE,
'onResize', ON_TAB_ITEM_TAP,
// 'onPageScroll', // 影响性能,开发者手动注册 ON_REACH_BOTTOM,
'onTabItemTap', ON_PULL_DOWN_REFRESH,
'onReachBottom', ON_ADD_TO_FAVORITES,
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites',
]; ];
function findHooks(vueOptions, hooks = new Set()) { function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) { if (vueOptions) {
...@@ -156,7 +192,7 @@ function initHook$1(mpOptions, hook, excludes) { ...@@ -156,7 +192,7 @@ function initHook$1(mpOptions, hook, excludes) {
}; };
} }
} }
const EXCLUDE_HOOKS = ['onReady']; const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes)); hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
} }
...@@ -165,12 +201,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { ...@@ -165,12 +201,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
} }
const HOOKS = [ const HOOKS = [
'onShow', ON_SHOW,
'onHide', ON_HIDE,
'onError', ON_ERROR,
'onThemeChange', ON_THEME_CHANGE,
'onPageNotFound', ON_PAGE_NOT_FOUND,
'onUnhandledRejection', ON_UNHANDLE_REJECTION,
]; ];
function parseApp(instance, parseAppOptions) { function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$; const internalInstance = instance.$;
...@@ -189,7 +225,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -189,7 +225,7 @@ function parseApp(instance, parseAppOptions) {
slots: [], slots: [],
}); });
ctx.globalData = this.globalData; ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options); instance.$callHook(ON_LAUNCH, options);
}, },
}; };
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
...@@ -210,26 +246,6 @@ function initCreateApp(parseAppOptions) { ...@@ -210,26 +246,6 @@ function initCreateApp(parseAppOptions) {
}; };
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) { function initBehavior(options) {
return Behavior(options); return Behavior(options);
} }
...@@ -793,7 +809,7 @@ function parsePage(vueOptions, parseOptions) { ...@@ -793,7 +809,7 @@ function parsePage(vueOptions, parseOptions) {
this.$page = { this.$page = {
fullPath: '/' + this.route + stringifyQuery(query), fullPath: '/' + this.route + stringifyQuery(query),
}; };
return this.$vm && this.$vm.$callHook('onLoad', query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
...@@ -833,7 +849,7 @@ function initHook(name, options) { ...@@ -833,7 +849,7 @@ function initHook(name, options) {
} }
} }
Page = function (options) { Page = function (options) {
initHook('onLoad', options); initHook(ON_LOAD, options);
return MPPage(options); return MPPage(options);
}; };
Component = function (options) { Component = function (options) {
...@@ -1010,7 +1026,7 @@ function handleLink({ detail: { vuePid, nodeId, webviewId }, }) { ...@@ -1010,7 +1026,7 @@ function handleLink({ detail: { vuePid, nodeId, webviewId }, }) {
} }
vm.$callSyncHook('created'); vm.$callSyncHook('created');
vm.$callHook('mounted'); vm.$callHook('mounted');
vm.$callHook('onReady'); vm.$callHook(ON_READY);
} }
function parse(componentOptions, { handleLink }) { function parse(componentOptions, { handleLink }) {
componentOptions.methods.__l = handleLink; componentOptions.methods.__l = handleLink;
...@@ -1033,7 +1049,7 @@ function initLifetimes(lifetimesOptions) { ...@@ -1033,7 +1049,7 @@ function initLifetimes(lifetimesOptions) {
if (this.$vm && lifetimesOptions.isPage(this)) { if (this.$vm && lifetimesOptions.isPage(this)) {
this.$vm.$callSyncHook('created'); this.$vm.$callSyncHook('created');
this.$vm.$callHook('mounted'); this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady'); this.$vm.$callHook(ON_READY);
} }
else { else {
this.is && console.warn(this.is + ' is not ready'); this.is && console.warn(this.is + ' is not ready');
......
此差异已折叠。
...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) { ...@@ -113,19 +113,55 @@ function initMocks(instance, mpInstance, mocks) {
}); });
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [ const PAGE_HOOKS = [
'onLoad', ON_LOAD,
'onShow', ON_SHOW,
// 'onReady', // lifetimes.ready ON_HIDE,
'onHide', ON_UNLOAD,
'onUnload', ON_RESIZE,
'onResize', ON_TAB_ITEM_TAP,
// 'onPageScroll', // 影响性能,开发者手动注册 ON_REACH_BOTTOM,
'onTabItemTap', ON_PULL_DOWN_REFRESH,
'onReachBottom', ON_ADD_TO_FAVORITES,
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites',
]; ];
function findHooks(vueOptions, hooks = new Set()) { function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) { if (vueOptions) {
...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) { ...@@ -153,7 +189,7 @@ function initHook$1(mpOptions, hook, excludes) {
}; };
} }
} }
const EXCLUDE_HOOKS = ['onReady']; const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes)); hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
} }
...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { ...@@ -162,12 +198,12 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
} }
const HOOKS = [ const HOOKS = [
'onShow', ON_SHOW,
'onHide', ON_HIDE,
'onError', ON_ERROR,
'onThemeChange', ON_THEME_CHANGE,
'onPageNotFound', ON_PAGE_NOT_FOUND,
'onUnhandledRejection', ON_UNHANDLE_REJECTION,
]; ];
function parseApp(instance, parseAppOptions) { function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$; const internalInstance = instance.$;
...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -186,7 +222,7 @@ function parseApp(instance, parseAppOptions) {
slots: [], slots: [],
}); });
ctx.globalData = this.globalData; ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options); instance.$callHook(ON_LAUNCH, options);
}, },
}; };
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) { ...@@ -207,26 +243,6 @@ function initCreateApp(parseAppOptions) {
}; };
} }
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) { function initBehavior(options) {
return Behavior(options); return Behavior(options);
} }
...@@ -784,7 +800,7 @@ function parsePage(vueOptions, parseOptions) { ...@@ -784,7 +800,7 @@ function parsePage(vueOptions, parseOptions) {
this.$page = { this.$page = {
fullPath: '/' + this.route + stringifyQuery(query), fullPath: '/' + this.route + stringifyQuery(query),
}; };
return this.$vm && this.$vm.$callHook('onLoad', query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
...@@ -824,7 +840,7 @@ function initHook(name, options) { ...@@ -824,7 +840,7 @@ function initHook(name, options) {
} }
} }
Page = function (options) { Page = function (options) {
initHook('onLoad', options); initHook(ON_LOAD, options);
return MPPage(options); return MPPage(options);
}; };
Component = function (options) { Component = function (options) {
...@@ -864,7 +880,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { ...@@ -864,7 +880,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800 // https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) { if (this.$vm) {
this.$vm.$callHook('mounted'); this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady'); this.$vm.$callHook(ON_READY);
} }
}, },
detached() { detached() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册