diff --git a/src/platforms/mp-alipay/runtime/index.js b/src/platforms/mp-alipay/runtime/index.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..895b2857ad226d8cdf9079035eddaf5a266c907a 100644 --- a/src/platforms/mp-alipay/runtime/index.js +++ b/src/platforms/mp-alipay/runtime/index.js @@ -0,0 +1,24 @@ +import { handleProps } from './wrapper/util' + +const MPComponent = Component + +function initHook (name, options) { + const oldHook = options[name] + options[name] = function (...args) { + const props = this.props + if (props && props['data-com-type'] === 'wx') { + handleProps(this) + } + if (oldHook) { + return oldHook.apply(this, args) + } + } +} + +if (!MPComponent.__$wrappered) { + MPComponent.__$wrappered = true + Component = function (options = {}) { + initHook('onInit', options) + return MPComponent(options) + } +} diff --git a/src/platforms/mp-alipay/runtime/wrapper/util.js b/src/platforms/mp-alipay/runtime/wrapper/util.js index 12bd8ab95e27c6ea8f5c6ff8ab28b63129ff341d..68ec31889a6ab744539d17556b56d45744132b19 100644 --- a/src/platforms/mp-alipay/runtime/wrapper/util.js +++ b/src/platforms/mp-alipay/runtime/wrapper/util.js @@ -108,24 +108,24 @@ export function initChildVues (mpInstance) { delete mpInstance._$childVues } -function handleProps (ref) { +export function handleProps (ref) { const eventProps = {} let refProps = ref.props const eventList = (refProps['data-event-list'] || '').split(',') // 初始化支付宝小程序组件事件 eventList.forEach(key => { - const handler = refProps[key] - const res = key.match(/^on([A-Z])(\S*)/) - const event = res && (res[1].toLowerCase() + res[2]) - refProps[key] = eventProps[key] = function () { - const props = Object.assign({}, refProps) - props[key] = handler - // 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式 - delete props['data-com-type'] - triggerEvent.bind({ props })(event, { - __args__: [...arguments] - }) - } + const handler = refProps[key] + const res = key.match(/^on([A-Z])(\S*)/) + const event = res && (res[1].toLowerCase() + res[2]) + refProps[key] = eventProps[key] = function () { + const props = Object.assign({}, refProps) + props[key] = handler + // 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式 + delete props['data-com-type'] + triggerEvent.bind({ props })(event, { + __args__: [...arguments] + }) + } }) // 处理 props 重写 Object.defineProperty(ref, 'props', { @@ -142,9 +142,6 @@ export function handleRef (ref) { if (!(ref && this.$vm)) { return } - if (ref.props['data-com-type'] === 'wx') { - handleProps(ref) - } const refName = ref.props['data-ref'] const refInForName = ref.props['data-ref-in-for'] if (refName) {