提交 337c3395 编写于 作者: fxy060608's avatar fxy060608

fix(runtime): 支持 vue-class-component

上级 4a19265c
...@@ -279,14 +279,10 @@ function initMocks (vm) { ...@@ -279,14 +279,10 @@ function initMocks (vm) {
}); });
} }
function initHooks (mpOptions, hooks, delay = false) { function initHooks (mpOptions, hooks) {
hooks.forEach(hook => { hooks.forEach(hook => {
mpOptions[hook] = function (args) { mpOptions[hook] = function (args) {
if (delay) { this.$vm.__call_hook(hook, args);
setTimeout(() => this.$vm.__call_hook(hook, args));
} else {
this.$vm.__call_hook(hook, args);
}
}; };
}); });
} }
...@@ -476,8 +472,7 @@ const hooks = [ ...@@ -476,8 +472,7 @@ const hooks = [
'onPageNotFound' 'onPageNotFound'
]; ];
function createApp (vueOptions) { function createApp (vm) {
vueOptions = vueOptions.default || vueOptions;
// 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin // 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin
Vue.mixin({ Vue.mixin({
beforeCreate () { beforeCreate () {
...@@ -505,21 +500,20 @@ function createApp (vueOptions) { ...@@ -505,21 +500,20 @@ function createApp (vueOptions) {
const appOptions = { const appOptions = {
onLaunch (args) { onLaunch (args) {
this.$vm = new Vue(Object.assign(vueOptions, { this.$vm = vm;
mpType: 'app',
mpInstance: this
}));
this.$vm.$mount(); this.$vm._isMounted = true;
setTimeout(() => this.$vm.__call_hook('onLaunch', args)); 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); App(appOptions);
return vueOptions return vm
} }
function triggerLink (mpInstance, vueOptions) { function triggerLink (mpInstance, vueOptions) {
...@@ -562,14 +556,21 @@ const hooks$1 = [ ...@@ -562,14 +556,21 @@ const hooks$1 = [
function createPage (vueOptions) { function createPage (vueOptions) {
vueOptions = vueOptions.default || vueOptions; vueOptions = vueOptions.default || vueOptions;
let VueComponent;
if (isFn(vueOptions)) {
VueComponent = vueOptions;
vueOptions = VueComponent.extendOptions;
} else {
VueComponent = Vue.extend(vueOptions);
}
const pageOptions = { const pageOptions = {
data: getData(vueOptions, Vue.prototype), data: getData(vueOptions, Vue.prototype),
onLoad (args) { onLoad (args) {
this.$vm = new Vue(Object.assign(vueOptions, { this.$vm = new VueComponent({
mpType: 'page', mpType: 'page',
mpInstance: this mpInstance: this
})); });
this.$vm.__call_hook('created'); this.$vm.__call_hook('created');
this.$vm.__call_hook('onLoad', args); // 开发者可能会在 onLoad 时赋值,提前到 mount 之前 this.$vm.__call_hook('onLoad', args); // 开发者可能会在 onLoad 时赋值,提前到 mount 之前
......
{ {
"name": "@dcloudio/uni-mp-weixin", "name": "@dcloudio/uni-mp-weixin",
"version": "0.0.915", "version": "0.0.916",
"description": "uni-app mp-weixin", "description": "uni-app mp-weixin",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
......
...@@ -13,8 +13,7 @@ const hooks = [ ...@@ -13,8 +13,7 @@ const hooks = [
'onPageNotFound' 'onPageNotFound'
] ]
export function createApp (vueOptions) { export function createApp (vm) {
vueOptions = vueOptions.default || vueOptions
// 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin // 外部初始化时 Vue 还未初始化,放到 createApp 内部初始化 mixin
Vue.mixin({ Vue.mixin({
beforeCreate () { beforeCreate () {
...@@ -42,19 +41,18 @@ export function createApp (vueOptions) { ...@@ -42,19 +41,18 @@ export function createApp (vueOptions) {
const appOptions = { const appOptions = {
onLaunch (args) { onLaunch (args) {
this.$vm = new Vue(Object.assign(vueOptions, { this.$vm = vm
mpType: 'app',
mpInstance: this
}))
this.$vm.$mount() this.$vm._isMounted = true
setTimeout(() => this.$vm.__call_hook('onLaunch', args)) 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) App(appOptions)
return vueOptions return vm
} }
import Vue from 'vue' import Vue from 'vue'
import {
isFn
} from 'uni-shared'
import { import {
handleLink handleLink
} from 'uni-platform/runtime/wrapper/index' } from 'uni-platform/runtime/wrapper/index'
...@@ -29,6 +33,13 @@ const hooks = [ ...@@ -29,6 +33,13 @@ const hooks = [
export function createPage (vueOptions) { export function createPage (vueOptions) {
vueOptions = vueOptions.default || vueOptions vueOptions = vueOptions.default || vueOptions
let VueComponent
if (isFn(vueOptions)) {
VueComponent = vueOptions
vueOptions = VueComponent.extendOptions
} else {
VueComponent = Vue.extend(vueOptions)
}
const pageOptions = { const pageOptions = {
data: getData(vueOptions, Vue.prototype), data: getData(vueOptions, Vue.prototype),
onLoad (args) { onLoad (args) {
...@@ -36,10 +47,10 @@ export function createPage (vueOptions) { ...@@ -36,10 +47,10 @@ export function createPage (vueOptions) {
this.$baiduComponentInstances = Object.create(null) this.$baiduComponentInstances = Object.create(null)
} }
this.$vm = new Vue(Object.assign(vueOptions, { this.$vm = new VueComponent({
mpType: 'page', mpType: 'page',
mpInstance: this mpInstance: this
})) })
this.$vm.__call_hook('created') this.$vm.__call_hook('created')
this.$vm.__call_hook('onLoad', args) // 开发者可能会在 onLoad 时赋值,提前到 mount 之前 this.$vm.__call_hook('onLoad', args) // 开发者可能会在 onLoad 时赋值,提前到 mount 之前
......
...@@ -16,14 +16,10 @@ export function initMocks (vm) { ...@@ -16,14 +16,10 @@ export function initMocks (vm) {
}) })
} }
export function initHooks (mpOptions, hooks, delay = false) { export function initHooks (mpOptions, hooks) {
hooks.forEach(hook => { hooks.forEach(hook => {
mpOptions[hook] = function (args) { mpOptions[hook] = function (args) {
if (delay) { this.$vm.__call_hook(hook, args)
setTimeout(() => this.$vm.__call_hook(hook, args))
} else {
this.$vm.__call_hook(hook, args)
}
} }
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册