From d9d838a1a1c0916c388463e27ab5339dd48c9f0e Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 17 May 2019 18:26:40 +0800 Subject: [PATCH] fix(mp): alipay parent,children --- packages/uni-mp-alipay/dist/index.js | 57 +++++++++++-------- .../app-plus/service/api/sub-nvue.js | 34 +++++------ .../runtime/wrapper/component-parser.js | 13 +++-- .../mp-alipay/runtime/wrapper/page-parser.js | 3 +- .../mp-alipay/runtime/wrapper/util.js | 17 ++++-- 5 files changed, 74 insertions(+), 50 deletions(-) diff --git a/packages/uni-mp-alipay/dist/index.js b/packages/uni-mp-alipay/dist/index.js index 6ef9cfdeb..6df0b7e3b 100644 --- a/packages/uni-mp-alipay/dist/index.js +++ b/packages/uni-mp-alipay/dist/index.js @@ -40,7 +40,7 @@ const camelize = cached((str) => { return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') }); -const SYNC_API_RE = /subNVue|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/; +const SYNC_API_RE = /subNVue|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; @@ -88,17 +88,17 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; + /* eslint-disable no-extend-native */ + if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; } })) } @@ -1271,12 +1271,16 @@ function initRelation$1 (detail) { } function initChildVues (mpInstance) { + // 此时需保证当前 mpInstance 已经存在 $vm + if (!mpInstance.$vm) { + return + } mpInstance._$childVues && mpInstance._$childVues.forEach(({ vuePid, vueOptions, VueComponent, mpInstance: childMPInstance - }) => { + }) => { // 父子关系 handleLink.call(mpInstance, { detail: { @@ -1285,12 +1289,15 @@ function initChildVues (mpInstance) { } }); - childMPInstance.$vm = new VueComponent(vueOptions); - + childMPInstance.$vm = new VueComponent(vueOptions); + handleRef.call(vueOptions.parent.$scope, childMPInstance); - childMPInstance.$vm.$mount(); + childMPInstance.$vm.$mount(); + + initChildVues(childMPInstance); + console.log(childMPInstance.is, 'mounted'); childMPInstance.$vm._isMounted = true; childMPInstance.$vm.__call_hook('mounted'); childMPInstance.$vm.__call_hook('onReady'); @@ -1364,7 +1371,7 @@ const handleLink$1 = (function () { } } return function handleLink$$1 (detail) { - if (this.$vm) { // 父已初始化 + if (this.$vm && this.$vm._isMounted) { // 父已初始化 return handleLink.call(this, { detail: { vuePid: detail.vuePid, @@ -1443,7 +1450,8 @@ function parsePage (vuePageOptions) { this.$vm.__call_hook('onLoad', args); }, onReady () { - initChildVues(this); + initChildVues(this); + console.log(this.route, 'mounted'); this.$vm._isMounted = true; this.$vm.__call_hook('mounted'); this.$vm.__call_hook('onReady'); @@ -1494,22 +1502,25 @@ function initVm (VueComponent) { // 触发首次 setData this.$vm.$mount(); - } else { - initChildVues(this); + } else { // 处理父子关系 initRelation$1.call(this, { vuePid: this._$vuePid, vueOptions: options, VueComponent, mpInstance: this - }); - if (options.parent) { // 父组件已经初始化,直接初始化子,否则放到父组件的 didMount 中处理 + }); + + if (options.parent) { // 父组件已经初始化,直接初始化子,否则放到父组件的 didMount 中处理 // 初始化 vue 实例 this.$vm = new VueComponent(options); handleRef.call(options.parent.$scope, this); // 触发首次 setData - this.$vm.$mount(); + this.$vm.$mount(); + + initChildVues(this); + console.log(this.is, 'mounted'); this.$vm._isMounted = true; this.$vm.__call_hook('mounted'); this.$vm.__call_hook('onReady'); diff --git a/src/platforms/app-plus/service/api/sub-nvue.js b/src/platforms/app-plus/service/api/sub-nvue.js index 2ee50cd4f..8e14f148d 100644 --- a/src/platforms/app-plus/service/api/sub-nvue.js +++ b/src/platforms/app-plus/service/api/sub-nvue.js @@ -1,5 +1,23 @@ function wrapper (webview) { webview.$processed = true + + webview.postMessage = function (data) { + plus.webview.postMessageToUniNView({ + type: 'UniAppSubNVue', + data, + options: { + id: webview.id + } + }, webview.id) + } + let callbacks = [] + webview.onMessage = function (callback) { + callbacks.push(callback) + } + webview.$consumeMessage = function (e) { + callbacks.forEach(callback => callback(e)) + } + if (!webview.__uniapp_mask_id) { return } @@ -32,22 +50,6 @@ function wrapper (webview) { callbacks = [] return oldClose.apply(webview, args) } - webview.postMessage = function (data) { - plus.webview.postMessageToUniNView({ - type: 'UniAppSubNVue', - data, - options: { - id: webview.id - } - }, webview.id) - } - let callbacks = [] - webview.onMessage = function (callback) { - callbacks.push(callback) - } - webview.$consumeMessage = function (e) { - callbacks.forEach(callback => callback(e)) - } } export const subNVue = { diff --git a/src/platforms/mp-alipay/runtime/wrapper/component-parser.js b/src/platforms/mp-alipay/runtime/wrapper/component-parser.js index f86f87f0e..91e567350 100644 --- a/src/platforms/mp-alipay/runtime/wrapper/component-parser.js +++ b/src/platforms/mp-alipay/runtime/wrapper/component-parser.js @@ -46,22 +46,25 @@ function initVm (VueComponent) { // 触发首次 setData this.$vm.$mount() - } else { - initChildVues(this) + } else { // 处理父子关系 initRelation.call(this, { vuePid: this._$vuePid, vueOptions: options, VueComponent, mpInstance: this - }) - if (options.parent) { // 父组件已经初始化,直接初始化子,否则放到父组件的 didMount 中处理 + }) + + if (options.parent) { // 父组件已经初始化,直接初始化子,否则放到父组件的 didMount 中处理 // 初始化 vue 实例 this.$vm = new VueComponent(options) handleRef.call(options.parent.$scope, this) // 触发首次 setData - this.$vm.$mount() + this.$vm.$mount() + + initChildVues(this) + console.log(this.is, 'mounted') this.$vm._isMounted = true this.$vm.__call_hook('mounted') this.$vm.__call_hook('onReady') diff --git a/src/platforms/mp-alipay/runtime/wrapper/page-parser.js b/src/platforms/mp-alipay/runtime/wrapper/page-parser.js index c8995bef7..21573cf73 100644 --- a/src/platforms/mp-alipay/runtime/wrapper/page-parser.js +++ b/src/platforms/mp-alipay/runtime/wrapper/page-parser.js @@ -53,7 +53,8 @@ export default function parsePage (vuePageOptions) { this.$vm.__call_hook('onLoad', args) }, onReady () { - initChildVues(this) + initChildVues(this) + console.log(this.route, 'mounted') this.$vm._isMounted = true this.$vm.__call_hook('mounted') this.$vm.__call_hook('onReady') diff --git a/src/platforms/mp-alipay/runtime/wrapper/util.js b/src/platforms/mp-alipay/runtime/wrapper/util.js index 4ca694d9c..6af6aeefb 100644 --- a/src/platforms/mp-alipay/runtime/wrapper/util.js +++ b/src/platforms/mp-alipay/runtime/wrapper/util.js @@ -43,12 +43,16 @@ export function initRelation (detail) { } export function initChildVues (mpInstance) { + // 此时需保证当前 mpInstance 已经存在 $vm + if (!mpInstance.$vm) { + return + } mpInstance._$childVues && mpInstance._$childVues.forEach(({ vuePid, vueOptions, VueComponent, mpInstance: childMPInstance - }) => { + }) => { // 父子关系 handleBaseLink.call(mpInstance, { detail: { @@ -57,12 +61,15 @@ export function initChildVues (mpInstance) { } }) - childMPInstance.$vm = new VueComponent(vueOptions) - + childMPInstance.$vm = new VueComponent(vueOptions) + handleRef.call(vueOptions.parent.$scope, childMPInstance) - childMPInstance.$vm.$mount() + childMPInstance.$vm.$mount() + + initChildVues(childMPInstance) + console.log(childMPInstance.is, 'mounted') childMPInstance.$vm._isMounted = true childMPInstance.$vm.__call_hook('mounted') childMPInstance.$vm.__call_hook('onReady') @@ -136,7 +143,7 @@ export const handleLink = (function () { } } return function handleLink (detail) { - if (this.$vm) { // 父已初始化 + if (this.$vm && this.$vm._isMounted) { // 父已初始化 return handleBaseLink.call(this, { detail: { vuePid: detail.vuePid, -- GitLab