From 337c339517047c3035b348b39541b25cbd2b7b98 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 28 Mar 2019 16:34:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(runtime):=20=E6=94=AF=E6=8C=81=20vue-class-?= =?UTF-8?q?component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-weixin/dist/index.js | 37 +++++++++++++------------ packages/uni-mp-weixin/package.json | 2 +- src/core/runtime/wrapper/create-app.js | 18 ++++++------ src/core/runtime/wrapper/create-page.js | 15 ++++++++-- src/core/runtime/wrapper/util.js | 8 ++---- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index 3a1a237c76..dabdfd2325 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -279,14 +279,10 @@ function initMocks (vm) { }); } -function initHooks (mpOptions, hooks, delay = false) { +function initHooks (mpOptions, hooks) { hooks.forEach(hook => { mpOptions[hook] = function (args) { - if (delay) { - setTimeout(() => this.$vm.__call_hook(hook, args)); - } else { - this.$vm.__call_hook(hook, args); - } + this.$vm.__call_hook(hook, args); }; }); } @@ -476,8 +472,7 @@ const hooks = [ 'onPageNotFound' ]; -function createApp (vueOptions) { - vueOptions = vueOptions.default || vueOptions; +function createApp (vm) { // 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin Vue.mixin({ beforeCreate () { @@ -505,21 +500,20 @@ function createApp (vueOptions) { const appOptions = { onLaunch (args) { - this.$vm = new Vue(Object.assign(vueOptions, { - mpType: 'app', - mpInstance: this - })); + this.$vm = vm; - this.$vm.$mount(); - setTimeout(() => this.$vm.__call_hook('onLaunch', args)); + this.$vm._isMounted = true; + this.$vm.__call_hook('mounted'); + + this.$vm.__call_hook('onLaunch', args); } }; - initHooks(appOptions, hooks, true); // 延迟执行,因为 App 的注册在 main.js 之前,可能导致生命周期内 Vue 原型上开发者注册的属性无法访问 + initHooks(appOptions, hooks); // 延迟执行,因为 App 的注册在 main.js 之前,可能导致生命周期内 Vue 原型上开发者注册的属性无法访问 App(appOptions); - return vueOptions + return vm } function triggerLink (mpInstance, vueOptions) { @@ -562,14 +556,21 @@ const hooks$1 = [ function createPage (vueOptions) { vueOptions = vueOptions.default || vueOptions; + let VueComponent; + if (isFn(vueOptions)) { + VueComponent = vueOptions; + vueOptions = VueComponent.extendOptions; + } else { + VueComponent = Vue.extend(vueOptions); + } const pageOptions = { data: getData(vueOptions, Vue.prototype), onLoad (args) { - this.$vm = new Vue(Object.assign(vueOptions, { + this.$vm = new VueComponent({ mpType: 'page', mpInstance: this - })); + }); this.$vm.__call_hook('created'); this.$vm.__call_hook('onLoad', args); // 开发者可能会在 onLoad 时赋值,提前到 mount 之前 diff --git a/packages/uni-mp-weixin/package.json b/packages/uni-mp-weixin/package.json index 4af6745412..2eae6cffd1 100644 --- a/packages/uni-mp-weixin/package.json +++ b/packages/uni-mp-weixin/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-mp-weixin", - "version": "0.0.915", + "version": "0.0.916", "description": "uni-app mp-weixin", "main": "dist/index.js", "scripts": { diff --git a/src/core/runtime/wrapper/create-app.js b/src/core/runtime/wrapper/create-app.js index e307757f6b..0f44c4e489 100644 --- a/src/core/runtime/wrapper/create-app.js +++ b/src/core/runtime/wrapper/create-app.js @@ -13,8 +13,7 @@ const hooks = [ 'onPageNotFound' ] -export function createApp (vueOptions) { - vueOptions = vueOptions.default || vueOptions +export function createApp (vm) { // 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin Vue.mixin({ beforeCreate () { @@ -42,19 +41,18 @@ export function createApp (vueOptions) { const appOptions = { onLaunch (args) { - this.$vm = new Vue(Object.assign(vueOptions, { - mpType: 'app', - mpInstance: this - })) + this.$vm = vm - this.$vm.$mount() - setTimeout(() => this.$vm.__call_hook('onLaunch', args)) + this.$vm._isMounted = true + this.$vm.__call_hook('mounted') + + this.$vm.__call_hook('onLaunch', args) } } - initHooks(appOptions, hooks, true) // 延迟执行,因为 App 的注册在 main.js 之前,可能导致生命周期内 Vue 原型上开发者注册的属性无法访问 + initHooks(appOptions, hooks) // 延迟执行,因为 App 的注册在 main.js 之前,可能导致生命周期内 Vue 原型上开发者注册的属性无法访问 App(appOptions) - return vueOptions + return vm } diff --git a/src/core/runtime/wrapper/create-page.js b/src/core/runtime/wrapper/create-page.js index 721082dbfd..55acf5ad90 100644 --- a/src/core/runtime/wrapper/create-page.js +++ b/src/core/runtime/wrapper/create-page.js @@ -1,5 +1,9 @@ import Vue from 'vue' +import { + isFn +} from 'uni-shared' + import { handleLink } from 'uni-platform/runtime/wrapper/index' @@ -29,6 +33,13 @@ const hooks = [ export function createPage (vueOptions) { vueOptions = vueOptions.default || vueOptions + let VueComponent + if (isFn(vueOptions)) { + VueComponent = vueOptions + vueOptions = VueComponent.extendOptions + } else { + VueComponent = Vue.extend(vueOptions) + } const pageOptions = { data: getData(vueOptions, Vue.prototype), onLoad (args) { @@ -36,10 +47,10 @@ export function createPage (vueOptions) { this.$baiduComponentInstances = Object.create(null) } - this.$vm = new Vue(Object.assign(vueOptions, { + this.$vm = new VueComponent({ mpType: 'page', mpInstance: this - })) + }) this.$vm.__call_hook('created') this.$vm.__call_hook('onLoad', args) // 开发者可能会在 onLoad 时赋值,提前到 mount 之前 diff --git a/src/core/runtime/wrapper/util.js b/src/core/runtime/wrapper/util.js index 2da9d61f86..ec2cf50697 100644 --- a/src/core/runtime/wrapper/util.js +++ b/src/core/runtime/wrapper/util.js @@ -16,14 +16,10 @@ export function initMocks (vm) { }) } -export function initHooks (mpOptions, hooks, delay = false) { +export function initHooks (mpOptions, hooks) { hooks.forEach(hook => { mpOptions[hook] = function (args) { - if (delay) { - setTimeout(() => this.$vm.__call_hook(hook, args)) - } else { - this.$vm.__call_hook(hook, args) - } + this.$vm.__call_hook(hook, args) } }) } -- GitLab