Skip to content

Commit 0fe431b

Browse files
committed
optimize components with no data option
1 parent 7ffa77f commit 0fe431b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/core/instance/state.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export function initState (vm: Component) {
2626
const opts = vm.$options
2727
if (opts.props) initProps(vm, opts.props)
2828
if (opts.methods) initMethods(vm, opts.methods)
29-
initData(vm)
29+
if (opts.data) {
30+
initData(vm)
31+
} else {
32+
observe(vm._data = {}, true /* asRootData */)
33+
}
3034
if (opts.computed) initComputed(vm, opts.computed)
3135
if (opts.watch) initWatch(vm, opts.watch)
3236
}
@@ -96,8 +100,7 @@ function initData (vm: Component) {
96100
}
97101
}
98102
// observe data
99-
observe(data)
100-
data.__ob__ && data.__ob__.vmCount++
103+
observe(data, true /* asRootData */)
101104
}
102105

103106
const computedSharedDefinition = {

src/core/observer/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function copyAugment (target: Object, src: Object, keys: Array<string>) {
103103
* returns the new observer if successfully observed,
104104
* or the existing observer if the value already has one.
105105
*/
106-
export function observe (value: any): Observer | void {
106+
export function observe (value: any, asRootData: ?boolean): Observer | void {
107107
if (!isObject(value)) {
108108
return
109109
}
@@ -119,6 +119,9 @@ export function observe (value: any): Observer | void {
119119
) {
120120
ob = new Observer(value)
121121
}
122+
if (asRootData && ob) {
123+
ob.vmCount++
124+
}
122125
return ob
123126
}
124127

0 commit comments

Comments
 (0)