From 0fe431b490b90a412ade6e8c4b8065b1f4189b3b Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 13 Dec 2016 21:22:30 -0500 Subject: [PATCH] optimize components with no data option --- src/core/instance/state.js | 9 ++++++--- src/core/observer/index.js | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 9d4814aa..c0a0a70c 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -26,7 +26,11 @@ export function initState (vm: Component) { const opts = vm.$options if (opts.props) initProps(vm, opts.props) if (opts.methods) initMethods(vm, opts.methods) - initData(vm) + if (opts.data) { + initData(vm) + } else { + observe(vm._data = {}, true /* asRootData */) + } if (opts.computed) initComputed(vm, opts.computed) if (opts.watch) initWatch(vm, opts.watch) } @@ -96,8 +100,7 @@ function initData (vm: Component) { } } // observe data - observe(data) - data.__ob__ && data.__ob__.vmCount++ + observe(data, true /* asRootData */) } const computedSharedDefinition = { diff --git a/src/core/observer/index.js b/src/core/observer/index.js index 447430cc..fe1d4614 100644 --- a/src/core/observer/index.js +++ b/src/core/observer/index.js @@ -103,7 +103,7 @@ function copyAugment (target: Object, src: Object, keys: Array) { * returns the new observer if successfully observed, * or the existing observer if the value already has one. */ -export function observe (value: any): Observer | void { +export function observe (value: any, asRootData: ?boolean): Observer | void { if (!isObject(value)) { return } @@ -119,6 +119,9 @@ export function observe (value: any): Observer | void { ) { ob = new Observer(value) } + if (asRootData && ob) { + ob.vmCount++ + } return ob }