File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111 query ,
1212 hasOwn ,
1313 isReserved ,
14+ isPlainObject ,
1415 bind
1516} from '../../util/index'
1617
@@ -76,7 +77,13 @@ export default function (Vue) {
7677
7778 Vue . prototype . _initData = function ( ) {
7879 var dataFn = this . $options . data
79- var data = this . _data = dataFn ? dataFn ( ) || { } : { }
80+ var data = this . _data = dataFn ? dataFn ( ) : { }
81+ if ( ! isPlainObject ( data ) ) {
82+ data = { }
83+ process . env . NODE_ENV !== 'production' && warn (
84+ 'data functions should return an object.'
85+ )
86+ }
8087 var props = this . _props
8188 var runtimeData = this . _runtimeData
8289 ? typeof this . _runtimeData === 'function'
Original file line number Diff line number Diff line change @@ -2,6 +2,13 @@ var Vue = require('src')
22var _ = require ( 'src/util' )
33
44describe ( 'Instance state initialization' , function ( ) {
5+ it ( 'should warn data functions that do not return an object' , function ( ) {
6+ new Vue ( {
7+ data : function ( ) { }
8+ } )
9+ expect ( 'should return an object' ) . toHaveBeenWarned ( )
10+ } )
11+
512 describe ( 'data proxy' , function ( ) {
613 var data = {
714 a : 0 ,
You can’t perform that action at this time.
0 commit comments