@@ -40,8 +40,11 @@ function getGlobalData (app, rootVueVM) {
40
40
}
41
41
}
42
42
43
+ /**
44
+ * 格式化 properties 属性,并给每个属性加上 observer 方法
45
+ */
43
46
function normalizeProperties ( vm ) {
44
- const properties = vm . $options . properties
47
+ const properties = vm . $options . properties || { }
45
48
const res = { }
46
49
let val
47
50
for ( const key in properties ) {
@@ -52,7 +55,7 @@ function normalizeProperties (vm) {
52
55
type : val . type ,
53
56
value : val . value ,
54
57
observer ( newVal , oldVal ) {
55
- vm [ key ] = newVal
58
+ vm [ key ] = newVal // 先修改值再触发原始的 observer,跟 watch 行为保持一致
56
59
if ( typeof val . observer === 'function' ) {
57
60
val . observer . call ( vm , newVal , oldVal )
58
61
}
@@ -62,14 +65,17 @@ function normalizeProperties (vm) {
62
65
return res
63
66
}
64
67
68
+ /**
69
+ * 把 properties 中的属性 proxy 到 vm 上
70
+ */
65
71
function initMpProps ( vm ) {
66
72
const mpProps = vm . _mpProps = { }
67
73
const keys = Object . keys ( vm . $options . properties || { } )
68
74
keys . forEach ( key => {
69
75
if ( ! ( key in vm ) ) {
70
76
proxy ( vm , '_mpProps' , key )
77
+ mpProps [ key ] = undefined // for observe
71
78
}
72
- mpProps [ key ] = undefined // for observe
73
79
} )
74
80
observe ( mpProps , true )
75
81
}
@@ -140,8 +146,9 @@ export function initMP (mpType, next) {
140
146
initMpProps ( rootVueVM )
141
147
142
148
global . Component ( {
143
- // 页面的初始数据
149
+ // 小程序原生的组件属性
144
150
properties : normalizeProperties ( rootVueVM ) ,
151
+ // 页面的初始数据
145
152
data : {
146
153
$root : { }
147
154
} ,
0 commit comments