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

feat(mp): vue@3.0.0

上级 6d85aa0c
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -413,7 +449,20 @@ function addSafeAreaInsets(fromRes, toRes) {
bottom: fromRes.windowHeight - safeArea.bottom
};
}
}
}
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const getProvider = initGetProvider({
oauth: ['alipay'],
......@@ -1000,7 +1049,9 @@ var protocols = /*#__PURE__*/Object.freeze({
hideHomeButton: hideHomeButton,
saveImageToPhotosAlbum: saveImageToPhotosAlbum,
saveVideoToPhotosAlbum: saveVideoToPhotosAlbum,
chooseAddress: chooseAddress
chooseAddress: chooseAddress,
redirectTo: redirectTo,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -89,9 +67,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -100,7 +75,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -111,15 +88,8 @@ function initBaseInstance(instance, options) {
}
});
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -216,7 +186,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -247,6 +217,26 @@ 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) {
if (!vueIds) {
return;
......@@ -261,6 +251,16 @@ function initVueIds(vueIds, mpInstance) {
mpInstance._$vuePid = ids[1];
}
}
function initWxsCallMethods(methods, wxsCallMethods) {
if (!isArray(wxsCallMethods)) {
return;
}
wxsCallMethods.forEach((callMethod) => {
methods[callMethod] = function (args) {
return this.$vm[callMethod](args);
};
});
}
function findVmByVueId(instance, vuePid) {
// TODO vue3 中 没有 $children
const $children = instance.$children;
......@@ -971,11 +971,15 @@ function createVueComponent(mpType, mpInstance, vueOptions, parent) {
function createPage(vueOptions) {
vueOptions = vueOptions.default || vueOptions;
const pageOptions = {
onLoad(args) {
onLoad(query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
// 初始化 vue 实例
this.$vm = createVueComponent('page', this, vueOptions);
initSpecialMethods(this);
this.$vm.$callHook('onLoad', args);
this.$vm.$callHook('onLoad', query);
},
onReady() {
initChildVues(this);
......@@ -1003,6 +1007,7 @@ function createPage(vueOptions) {
}
initHooks(pageOptions, PAGE_HOOKS);
initUnknownHooks(pageOptions, vueOptions);
initWxsCallMethods(pageOptions, vueOptions.wxsCallMethods);
return Page(pageOptions);
}
......@@ -1099,6 +1104,7 @@ function createComponent(vueOptions) {
else {
mpComponentOptions.didUpdate = createObserver(true);
}
initWxsCallMethods(mpComponentOptions.methods, vueOptions.wxsCallMethods);
return Component(mpComponentOptions);
}
......
......@@ -1897,6 +1897,11 @@ function parsePage (vuePageOptions) {
initRelation
});
const onInit = (vuePageOptions.default || vuePageOptions).onInit;
if (onInit) {
pageOptions.methods.onInit = onInit;
}
// 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
pageOptions.methods.onShow = function onShow () {
if (this.$vm && this.$vm.$mp.query) {
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -450,7 +486,20 @@ function addSafeAreaInsets(fromRes, toRes) {
const getSystemInfo = {
returnValue: addSafeAreaInsets
};
const getSystemInfoSync = getSystemInfo;
const getSystemInfoSync = getSystemInfo;
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const getProvider = initGetProvider({
oauth: ['baidu'],
......@@ -565,9 +614,11 @@ var protocols = /*#__PURE__*/Object.freeze({
navigateBackMiniProgram: navigateBackMiniProgram,
showShareMenu: showShareMenu,
getAccountInfoSync: getAccountInfoSync,
redirectTo: redirectTo,
previewImage: previewImage,
getSystemInfo: getSystemInfo,
getSystemInfoSync: getSystemInfoSync
getSystemInfoSync: getSystemInfoSync,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -82,9 +60,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -93,7 +68,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -105,15 +82,8 @@ function initBaseInstance(instance, options) {
});
}
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -207,7 +177,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -238,6 +208,26 @@ 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) {
return Behavior(options);
}
......@@ -810,6 +800,13 @@ function parsePage(vueOptions, parseOptions) {
initLifetimes
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
return this.$vm && this.$vm.$callHook('onLoad', query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
parse && parse(miniProgramPageOptions, { handleLink });
......@@ -929,7 +926,7 @@ function handleLink(event) {
const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
function isPage(mpInstance) {
return !mpInstance.ownerId;
return !hasOwn(mpInstance, 'ownerId');
}
function initRelation(mpInstance, detail) {
mpInstance.dispatch('__l', detail);
......
......@@ -1317,7 +1317,9 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange',
'onUnhandledRejection'
];
function parseBaseApp (vm, {
......@@ -1393,17 +1395,19 @@ function parseBaseApp (vm, {
return appOptions
}
/* 快手也使用__wxExparserNodeId__和__wxWebviewId__ */
const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
function findVmByVueId (vm, vuePid) {
const $children = vm.$children;
// 优先查找直属
let parentVm = $children.find(childVm => childVm.$scope._$vueId === vuePid);
if (parentVm) {
return parentVm
// 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
for (let i = $children.length - 1; i >= 0; i--) {
const childVm = $children[i];
if (childVm.$scope._$vueId === vuePid) {
return childVm
}
}
// 反向递归查找
let parentVm;
for (let i = $children.length - 1; i >= 0; i--) {
parentVm = findVmByVueId($children[i], vuePid);
if (parentVm) {
......@@ -1473,6 +1477,10 @@ function parseApp (vm) {
})
}
function parseApp$1 (vm) {
return parseApp(vm)
}
function createApp (vm) {
Vue.prototype.getOpenerEventChannel = function () {
if (!this.__eventChannel__) {
......@@ -1488,10 +1496,53 @@ function createApp (vm) {
}
return callHook.call(this, hook, args)
};
App(parseApp(vm));
App(parseApp$1(vm));
return vm
}
const encodeReserveRE = /[!'()*]/g;
const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16);
const commaRE = /%2C/g;
// fixed encodeURIComponent which is more conformant to RFC3986:
// - escapes [!'()*]
// - preserve commas
const encode = str => encodeURIComponent(str)
.replace(encodeReserveRE, encodeReserveReplacer)
.replace(commaRE, ',');
function stringifyQuery (obj, encodeStr = encode) {
const res = obj ? Object.keys(obj).map(key => {
const val = obj[key];
if (val === undefined) {
return ''
}
if (val === null) {
return encodeStr(key)
}
if (Array.isArray(val)) {
const result = [];
val.forEach(val2 => {
if (val2 === undefined) {
return
}
if (val2 === null) {
result.push(encodeStr(key));
} else {
result.push(encodeStr(key) + '=' + encodeStr(val2));
}
});
return result.join('&')
}
return encodeStr(key) + '=' + encodeStr(val)
}).filter(x => x.length > 0).join('&') : null;
return res ? `?${res}` : ''
}
function parseBaseComponent (vueComponentOptions, {
isPage,
initRelation
......@@ -1500,7 +1551,8 @@ function parseBaseComponent (vueComponentOptions, {
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
...(vueOptions.options || {})
};
const componentOptions = {
......@@ -1545,7 +1597,7 @@ function parseBaseComponent (vueComponentOptions, {
}
},
detached () {
this.$vm.$destroy();
this.$vm && this.$vm.$destroy();
}
},
pageLifetimes: {
......@@ -1564,6 +1616,10 @@ function parseBaseComponent (vueComponentOptions, {
__e: handleEvent
}
};
// externalClasses
if (vueOptions.externalClasses) {
componentOptions.externalClasses = vueOptions.externalClasses;
}
if (Array.isArray(vueOptions.wxsCallMethods)) {
vueOptions.wxsCallMethods.forEach(callMethod => {
......@@ -1586,6 +1642,10 @@ function parseComponent (vueComponentOptions) {
})
}
function parseComponent$1 (vueComponentOptions) {
return parseComponent(vueComponentOptions)
}
const hooks$1 = [
'onShow',
'onHide',
......@@ -1598,13 +1658,19 @@ function parseBasePage (vuePageOptions, {
isPage,
initRelation
}) {
const pageOptions = parseComponent(vuePageOptions);
const pageOptions = parseComponent$1(vuePageOptions);
initHooks(pageOptions.methods, hooks$1, vuePageOptions);
pageOptions.methods.onLoad = function (args) {
this.$vm.$mp.query = args; // 兼容 mpvue
this.$vm.__call_hook('onLoad', args);
pageOptions.methods.onLoad = function (query) {
this.options = query;
const copyQuery = Object.assign({}, query);
delete copyQuery.__id__;
this.$page = {
fullPath: '/' + (this.route || this.is) + stringifyQuery(copyQuery)
};
this.$vm.$mp.query = query; // 兼容 mpvue
this.$vm.__call_hook('onLoad', query);
};
return pageOptions
......@@ -1617,15 +1683,19 @@ function parsePage (vuePageOptions) {
})
}
function parsePage$1 (vuePageOptions) {
return parsePage(vuePageOptions)
}
function createPage (vuePageOptions) {
{
return Component(parsePage(vuePageOptions))
return Component(parsePage$1(vuePageOptions))
}
}
function createComponent (vueOptions) {
{
return Component(parseComponent(vueOptions))
return Component(parseComponent$1(vueOptions))
}
}
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -450,7 +486,20 @@ function addSafeAreaInsets(fromRes, toRes) {
const getSystemInfo = {
returnValue: addSafeAreaInsets
};
const getSystemInfoSync = getSystemInfo;
const getSystemInfoSync = getSystemInfo;
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const getProvider = initGetProvider({
oauth: ['qq'],
......@@ -466,9 +515,11 @@ var shims = /*#__PURE__*/Object.freeze({
var protocols = /*#__PURE__*/Object.freeze({
__proto__: null,
redirectTo: redirectTo,
previewImage: previewImage,
getSystemInfo: getSystemInfo,
getSystemInfoSync: getSystemInfoSync
getSystemInfoSync: getSystemInfoSync,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -82,9 +60,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -93,7 +68,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -105,15 +82,8 @@ function initBaseInstance(instance, options) {
});
}
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -207,7 +177,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -238,6 +208,26 @@ 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) {
return Behavior(options);
}
......@@ -791,6 +781,13 @@ function parsePage(vueOptions, parseOptions) {
initLifetimes
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
return this.$vm && this.$vm.$callHook('onLoad', query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
parse && parse(miniProgramPageOptions, { handleLink });
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -435,6 +471,19 @@ const previewImage = {
loop: false
};
}
};
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const getProvider = initGetProvider({
......@@ -536,7 +585,9 @@ var protocols = /*#__PURE__*/Object.freeze({
getUserInfo: getUserInfo,
requestPayment: requestPayment,
getFileInfo: getFileInfo,
previewImage: previewImage
redirectTo: redirectTo,
previewImage: previewImage,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -82,9 +60,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -93,7 +68,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -105,15 +82,8 @@ function initBaseInstance(instance, options) {
});
}
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -210,7 +180,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -241,6 +211,26 @@ 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) {
return Behavior(options);
}
......@@ -800,6 +790,13 @@ function parsePage(vueOptions, parseOptions) {
initLifetimes
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
return this.$vm && this.$vm.$callHook('onLoad', query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
parse && parse(miniProgramPageOptions, { handleLink });
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -450,7 +486,20 @@ function addSafeAreaInsets(fromRes, toRes) {
const getSystemInfo = {
returnValue: addSafeAreaInsets
};
const getSystemInfoSync = getSystemInfo;
const getSystemInfoSync = getSystemInfo;
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const getProvider = initGetProvider({
oauth: ['weixin'],
......@@ -466,9 +515,11 @@ var shims = /*#__PURE__*/Object.freeze({
var protocols = /*#__PURE__*/Object.freeze({
__proto__: null,
redirectTo: redirectTo,
previewImage: previewImage,
getSystemInfo: getSystemInfo,
getSystemInfoSync: getSystemInfoSync
getSystemInfoSync: getSystemInfoSync,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -82,9 +60,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -93,7 +68,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -105,15 +82,8 @@ function initBaseInstance(instance, options) {
});
}
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -207,7 +177,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -238,6 +208,26 @@ 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) {
return Behavior(options);
}
......@@ -791,6 +781,13 @@ function parsePage(vueOptions, parseOptions) {
initLifetimes
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
return this.$vm && this.$vm.$callHook('onLoad', query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
parse && parse(miniProgramPageOptions, { handleLink });
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
const API_TYPE_SYNC = 1;
function validateProtocol(_name, _args, _protocol) {
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
{
name: 'upx',
type: [Number, String],
required: true
}
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS = false;
......@@ -11,7 +38,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
function upx2px(number, newDeviceWidth) {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -33,7 +60,7 @@ function upx2px(number, newDeviceWidth) {
}
}
return number < 0 ? -result : result;
}
}, Upx2pxProtocol);
var HOOKS;
(function (HOOKS) {
......@@ -139,6 +166,15 @@ function invokeApi(method, api, options, ...params) {
return api(options, ...params);
}
const AddInterceptorProtocol = [
{
name: 'method',
type: [String, Object],
required: true
}
];
const RemoveInterceptorProtocol = AddInterceptorProtocol;
function mergeInterceptorHook(interceptors, interceptor) {
Object.keys(interceptor).forEach(hook => {
if (isFunction(interceptor[hook])) {
......@@ -184,15 +220,15 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
function addInterceptor(method, interceptor) {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}
function removeInterceptor(method, interceptor) {
}, AddInterceptorProtocol);
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......@@ -204,7 +240,7 @@ function removeInterceptor(method, interceptor) {
else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}
}, RemoveInterceptorProtocol);
const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const CONTEXT_API_RE = /^create|Manager$/;
......@@ -450,7 +486,20 @@ function addSafeAreaInsets(fromRes, toRes) {
const getSystemInfo = {
returnValue: addSafeAreaInsets
};
const getSystemInfoSync = getSystemInfo;
const getSystemInfoSync = getSystemInfo;
const redirectTo = {};
const createCanvasContext = {
returnValue(fromRes, toRes) {
const measureText = fromRes.measureText;
toRes.measureText = function (text, callback) {
const textMetrics = measureText.call(this, text);
if (typeof callback === 'function') {
setTimeout(() => callback(textMetrics), 0);
}
return textMetrics;
};
}
};
const providers = {
oauth: [],
......@@ -473,9 +522,11 @@ var shims = /*#__PURE__*/Object.freeze({
var protocols = /*#__PURE__*/Object.freeze({
__proto__: null,
redirectTo: redirectTo,
previewImage: previewImage,
getSystemInfo: getSystemInfo,
getSystemInfoSync: getSystemInfoSync
getSystemInfoSync: getSystemInfoSync,
createCanvasContext: createCanvasContext
});
var index = initUni(shims, protocols);
......
......@@ -52,28 +52,6 @@ const MP_METHODS = [
'selectAllComponents',
'selectComponent'
];
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
const hooks = this.$[name];
let ret;
if (hooks) {
for (let i = 0; i < hooks.length; i++) {
ret = hooks[i](args);
}
}
return ret;
}
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
if (ctx.$scope && event) {
......@@ -82,9 +60,6 @@ function createEmitFn(oldEmit, ctx) {
return oldEmit.apply(this, [event, ...args]);
};
}
function set(target, key, val) {
return (target[key] = val);
}
function initBaseInstance(instance, options) {
const ctx = instance.ctx;
// mp
......@@ -93,7 +68,9 @@ function initBaseInstance(instance, options) {
ctx.$scope = options.mpInstance;
// TODO @deprecated
ctx.$mp = {};
ctx._self = {};
if (__VUE_OPTIONS_API__) {
ctx._self = {};
}
// $vm
ctx.$scope.$vm = instance.proxy;
// slots
......@@ -105,15 +82,8 @@ function initBaseInstance(instance, options) {
});
}
}
if (__VUE_OPTIONS_API__) {
// $set
ctx.$set = set;
}
// $emit
instance.emit = createEmitFn(instance.emit, ctx);
// $callHook
ctx.$hasHook = hasHook;
ctx.$callHook = callHook;
}
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
......@@ -207,7 +177,7 @@ function parseApp(instance, parseAppOptions) {
$vm: instance,
onLaunch(options) {
const ctx = internalInstance.ctx;
if (this.$vm && ctx.$callHook) {
if (this.$vm && ctx.$scope) {
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return;
}
......@@ -238,6 +208,26 @@ 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) {
return Behavior(options);
}
......@@ -772,6 +762,13 @@ function parsePage(vueOptions, parseOptions) {
initLifetimes
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
};
return this.$vm && this.$vm.$callHook('onLoad', query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
parse && parse(miniProgramPageOptions, { handleLink });
......@@ -969,7 +966,7 @@ function initLifetimes$1(lifetimesOptions) {
const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
function isPage(mpInstance) {
return !mpInstance.ownerId;
return !hasOwn(mpInstance, 'ownerId');
}
function initRelation(mpInstance) {
......
......@@ -58,7 +58,7 @@ module.exports = (api, options, rootOptions) => {
'@dcloudio/uni-quickapp-native': version,
'@dcloudio/uni-quickapp-webview': version,
'@dcloudio/uni-stat': version,
'@vue/shared': '^3.0.0-rc.4', // TODO
'@vue/shared': '^3.0.0',
flyio: '^0.6.2',
vuex: '^3.2.0'
},
......
......@@ -29,6 +29,11 @@ export default function parsePage (vuePageOptions) {
initRelation
})
const onInit = (vuePageOptions.default || vuePageOptions).onInit
if (onInit) {
pageOptions.methods.onInit = onInit
}
// 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
pageOptions.methods.onShow = function onShow () {
if (this.$vm && this.$vm.$mp.query) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册