|
1 |
| -import { handleError } from '../../../core/util/index' |
| 1 | +import { isPlainObject } from 'shared/util' |
| 2 | +import { handleError } from 'core/util/index' |
| 3 | +import { observe } from 'core/observer/index' |
| 4 | +import { proxy } from 'core/instance/state' |
2 | 5 |
|
3 | 6 | export function callHook (vm, hook, params) {
|
4 | 7 | let handlers = vm.$options[hook]
|
@@ -37,6 +40,40 @@ function getGlobalData (app, rootVueVM) {
|
37 | 40 | }
|
38 | 41 | }
|
39 | 42 |
|
| 43 | +function normalizeProperties (vm) { |
| 44 | + const properties = vm.$options.properties |
| 45 | + const res = {} |
| 46 | + let val |
| 47 | + for (const key in properties) { |
| 48 | + val = isPlainObject(properties[key]) |
| 49 | + ? properties[key] |
| 50 | + : { type: properties[key] } |
| 51 | + res[key] = { |
| 52 | + type: val.type, |
| 53 | + value: val.value, |
| 54 | + observer (newVal, oldVal) { |
| 55 | + vm[key] = newVal |
| 56 | + if (typeof val.observer === 'function') { |
| 57 | + val.observer.call(vm, newVal, oldVal) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return res |
| 63 | +} |
| 64 | + |
| 65 | +function initMpProps (vm) { |
| 66 | + const mpProps = vm._mpProps = {} |
| 67 | + const keys = Object.keys(vm.$options.properties || {}) |
| 68 | + keys.forEach(key => { |
| 69 | + if (!(key in vm)) { |
| 70 | + proxy(vm, '_mpProps', key) |
| 71 | + } |
| 72 | + mpProps[key] = undefined // for observe |
| 73 | + }) |
| 74 | + observe(mpProps, true) |
| 75 | +} |
| 76 | + |
40 | 77 | export function initMP (mpType, next) {
|
41 | 78 | const rootVueVM = this.$root
|
42 | 79 | if (!rootVueVM.$mp) {
|
@@ -100,8 +137,11 @@ export function initMP (mpType, next) {
|
100 | 137 | }
|
101 | 138 | })
|
102 | 139 | } else if (mpType === 'component') {
|
| 140 | + initMpProps(rootVueVM) |
| 141 | + |
103 | 142 | global.Component({
|
104 | 143 | // 页面的初始数据
|
| 144 | + properties: normalizeProperties(rootVueVM), |
105 | 145 | data: {
|
106 | 146 | $root: {}
|
107 | 147 | },
|
|
0 commit comments