@@ -40,8 +40,11 @@ function getGlobalData (app, rootVueVM) {
4040 }
4141}
4242
43+ /**
44+ * 格式化 properties 属性,并给每个属性加上 observer 方法
45+ */
4346function normalizeProperties ( vm ) {
44- const properties = vm . $options . properties
47+ const properties = vm . $options . properties || { }
4548 const res = { }
4649 let val
4750 for ( const key in properties ) {
@@ -52,7 +55,7 @@ function normalizeProperties (vm) {
5255 type : val . type ,
5356 value : val . value ,
5457 observer ( newVal , oldVal ) {
55- vm [ key ] = newVal
58+ vm [ key ] = newVal // 先修改值再触发原始的 observer,跟 watch 行为保持一致
5659 if ( typeof val . observer === 'function' ) {
5760 val . observer . call ( vm , newVal , oldVal )
5861 }
@@ -62,14 +65,17 @@ function normalizeProperties (vm) {
6265 return res
6366}
6467
68+ /**
69+ * 把 properties 中的属性 proxy 到 vm 上
70+ */
6571function initMpProps ( vm ) {
6672 const mpProps = vm . _mpProps = { }
6773 const keys = Object . keys ( vm . $options . properties || { } )
6874 keys . forEach ( key => {
6975 if ( ! ( key in vm ) ) {
7076 proxy ( vm , '_mpProps' , key )
77+ mpProps [ key ] = undefined // for observe
7178 }
72- mpProps [ key ] = undefined // for observe
7379 } )
7480 observe ( mpProps , true )
7581}
@@ -140,8 +146,9 @@ export function initMP (mpType, next) {
140146 initMpProps ( rootVueVM )
141147
142148 global . Component ( {
143- // 页面的初始数据
149+ // 小程序原生的组件属性
144150 properties : normalizeProperties ( rootVueVM ) ,
151+ // 页面的初始数据
145152 data : {
146153 $root : { }
147154 } ,
0 commit comments