@@ -23,50 +23,48 @@ import {
23
23
24
24
export function initState ( vm : Component ) {
25
25
vm . _watchers = [ ]
26
- initProps ( vm )
27
- initMethods ( vm )
26
+ const opts = vm . $options
27
+ if ( opts . props ) initProps ( vm , opts . props )
28
+ if ( opts . methods ) initMethods ( vm , opts . methods )
28
29
initData ( vm )
29
- initComputed ( vm )
30
- initWatch ( vm )
30
+ if ( opts . computed ) initComputed ( vm , opts . computed )
31
+ if ( opts . watch ) initWatch ( vm , opts . watch )
31
32
}
32
33
33
34
const isReservedProp = { key : 1 , ref : 1 , slot : 1 }
34
35
35
- function initProps ( vm : Component ) {
36
- const props = vm . $options . props
37
- if ( props ) {
38
- const propsData = vm . $options . propsData || { }
39
- const keys = vm . $options . _propKeys = Object . keys ( props )
40
- const isRoot = ! vm . $parent
41
- // root instance props should be converted
42
- observerState . shouldConvert = isRoot
43
- for ( let i = 0 ; i < keys . length ; i ++ ) {
44
- const key = keys [ i ]
45
- /* istanbul ignore else */
46
- if ( process . env . NODE_ENV !== 'production' ) {
47
- if ( isReservedProp [ key ] ) {
36
+ function initProps ( vm : Component , props : Object ) {
37
+ const propsData = vm . $options . propsData || { }
38
+ const keys = vm . $options . _propKeys = Object . keys ( props )
39
+ const isRoot = ! vm . $parent
40
+ // root instance props should be converted
41
+ observerState . shouldConvert = isRoot
42
+ for ( let i = 0 ; i < keys . length ; i ++ ) {
43
+ const key = keys [ i ]
44
+ /* istanbul ignore else */
45
+ if ( process . env . NODE_ENV !== 'production' ) {
46
+ if ( isReservedProp [ key ] ) {
47
+ warn (
48
+ `"${ key } " is a reserved attribute and cannot be used as component prop.` ,
49
+ vm
50
+ )
51
+ }
52
+ defineReactive ( vm , key , validateProp ( key , props , propsData , vm ) , ( ) => {
53
+ if ( vm . $parent && ! observerState . isSettingProps ) {
48
54
warn (
49
- `"${ key } " is a reserved attribute and cannot be used as component prop.` ,
55
+ `Avoid mutating a prop directly since the value will be ` +
56
+ `overwritten whenever the parent component re-renders. ` +
57
+ `Instead, use a data or computed property based on the prop's ` +
58
+ `value. Prop being mutated: "${ key } "` ,
50
59
vm
51
60
)
52
61
}
53
- defineReactive ( vm , key , validateProp ( key , props , propsData , vm ) , ( ) => {
54
- if ( vm . $parent && ! observerState . isSettingProps ) {
55
- warn (
56
- `Avoid mutating a prop directly since the value will be ` +
57
- `overwritten whenever the parent component re-renders. ` +
58
- `Instead, use a data or computed property based on the prop's ` +
59
- `value. Prop being mutated: "${ key } "` ,
60
- vm
61
- )
62
- }
63
- } )
64
- } else {
65
- defineReactive ( vm , key , validateProp ( key , props , propsData , vm ) )
66
- }
62
+ } )
63
+ } else {
64
+ defineReactive ( vm , key , validateProp ( key , props , propsData , vm ) )
67
65
}
68
- observerState . shouldConvert = true
69
66
}
67
+ observerState . shouldConvert = true
70
68
}
71
69
72
70
function initData ( vm : Component ) {
@@ -109,26 +107,23 @@ const computedSharedDefinition = {
109
107
set : noop
110
108
}
111
109
112
- function initComputed ( vm : Component ) {
113
- const computed = vm . $options . computed
114
- if ( computed ) {
115
- for ( const key in computed ) {
116
- const userDef = computed [ key ]
117
- if ( typeof userDef === 'function' ) {
118
- computedSharedDefinition . get = makeComputedGetter ( userDef , vm )
119
- computedSharedDefinition . set = noop
120
- } else {
121
- computedSharedDefinition . get = userDef . get
122
- ? userDef . cache !== false
123
- ? makeComputedGetter ( userDef . get , vm )
124
- : bind ( userDef . get , vm )
125
- : noop
126
- computedSharedDefinition . set = userDef . set
127
- ? bind ( userDef . set , vm )
128
- : noop
129
- }
130
- Object . defineProperty ( vm , key , computedSharedDefinition )
110
+ function initComputed ( vm : Component , computed : Object ) {
111
+ for ( const key in computed ) {
112
+ const userDef = computed [ key ]
113
+ if ( typeof userDef === 'function' ) {
114
+ computedSharedDefinition . get = makeComputedGetter ( userDef , vm )
115
+ computedSharedDefinition . set = noop
116
+ } else {
117
+ computedSharedDefinition . get = userDef . get
118
+ ? userDef . cache !== false
119
+ ? makeComputedGetter ( userDef . get , vm )
120
+ : bind ( userDef . get , vm )
121
+ : noop
122
+ computedSharedDefinition . set = userDef . set
123
+ ? bind ( userDef . set , vm )
124
+ : noop
131
125
}
126
+ Object . defineProperty ( vm , key , computedSharedDefinition )
132
127
}
133
128
}
134
129
@@ -147,34 +142,28 @@ function makeComputedGetter (getter: Function, owner: Component): Function {
147
142
}
148
143
}
149
144
150
- function initMethods ( vm : Component ) {
151
- const methods = vm . $options . methods
152
- if ( methods ) {
153
- for ( const key in methods ) {
154
- vm [ key ] = methods [ key ] == null ? noop : bind ( methods [ key ] , vm )
155
- if ( process . env . NODE_ENV !== 'production' && methods [ key ] == null ) {
156
- warn (
157
- `method "${ key } " has an undefined value in the component definition. ` +
158
- `Did you reference the function correctly?` ,
159
- vm
160
- )
161
- }
145
+ function initMethods ( vm : Component , methods : Object ) {
146
+ for ( const key in methods ) {
147
+ vm [ key ] = methods [ key ] == null ? noop : bind ( methods [ key ] , vm )
148
+ if ( process . env . NODE_ENV !== 'production' && methods [ key ] == null ) {
149
+ warn (
150
+ `method "${ key } " has an undefined value in the component definition. ` +
151
+ `Did you reference the function correctly?` ,
152
+ vm
153
+ )
162
154
}
163
155
}
164
156
}
165
157
166
- function initWatch ( vm : Component ) {
167
- const watch = vm . $options . watch
168
- if ( watch ) {
169
- for ( const key in watch ) {
170
- const handler = watch [ key ]
171
- if ( Array . isArray ( handler ) ) {
172
- for ( let i = 0 ; i < handler . length ; i ++ ) {
173
- createWatcher ( vm , key , handler [ i ] )
174
- }
175
- } else {
176
- createWatcher ( vm , key , handler )
158
+ function initWatch ( vm : Component , watch : Object ) {
159
+ for ( const key in watch ) {
160
+ const handler = watch [ key ]
161
+ if ( Array . isArray ( handler ) ) {
162
+ for ( let i = 0 ; i < handler . length ; i ++ ) {
163
+ createWatcher ( vm , key , handler [ i ] )
177
164
}
165
+ } else {
166
+ createWatcher ( vm , key , handler )
178
167
}
179
168
}
180
169
}
0 commit comments