@@ -44,7 +44,8 @@ function getGlobalData (app, rootVueVM) {
44
44
* 格式化 properties 属性,并给每个属性加上 observer 方法
45
45
*/
46
46
function normalizeProperties ( vm ) {
47
- const properties = vm . $options . properties || vm . $options . props || { }
47
+ const properties = vm . $options . properties || { }
48
+ const vueProps = vm . $options . props || { }
48
49
const res = { }
49
50
let val
50
51
for ( const key in properties ) {
@@ -62,6 +63,38 @@ function normalizeProperties (vm) {
62
63
}
63
64
}
64
65
}
66
+
67
+ // 如果是以vue props写法定义属性
68
+ // 纯数组定义属性 ['title', 'likes', 'isPublished', 'commentIds', 'author']
69
+ if ( Array . isArray ( vueProps ) ) {
70
+ for ( const prop of vueProps ) {
71
+ // vue props定义的属性不覆盖properties原生形式定义的属性
72
+ if ( ! res [ prop ] ) {
73
+ res [ prop ] = {
74
+ type : null
75
+ }
76
+ }
77
+ }
78
+ } else {
79
+ for ( const key in vueProps ) {
80
+ val = isPlainObject ( vueProps [ key ] )
81
+ ? vueProps [ key ]
82
+ : { type : vueProps [ key ] }
83
+ res [ key ] = {
84
+ // vue props的type可以是数组代表支持多种类型,但是小程序不支持,因此切换为任意类型
85
+ type : Array . isArray ( val . type ) ? null : val . type ,
86
+ value : typeof val . default === 'function' ? val . default ( ) : val . default ,
87
+ observer ( newVal , oldVal ) {
88
+ vm [ key ] = newVal
89
+ // 上面的赋值自带watch方法执行了,无需再调用watch方法
90
+ // if (typeof watches[key] === 'function') {
91
+ // watches[key].call(vm, newVal, oldVal)
92
+ // }
93
+ }
94
+ }
95
+ }
96
+ }
97
+
65
98
return res
66
99
}
67
100
0 commit comments