File tree 2 files changed +27
-6
lines changed
2 files changed +27
-6
lines changed Original file line number Diff line number Diff line change @@ -7,18 +7,19 @@ import {
7
7
set ,
8
8
del ,
9
9
observe ,
10
- defineReactive ,
11
- observerState
10
+ observerState ,
11
+ defineReactive
12
12
} from '../observer/index'
13
13
14
14
import {
15
15
warn ,
16
+ bind ,
17
+ noop ,
16
18
hasOwn ,
17
19
isReserved ,
18
- isPlainObject ,
19
- bind ,
20
+ handleError ,
20
21
validateProp ,
21
- noop
22
+ isPlainObject
22
23
} from '../util/index'
23
24
24
25
const sharedPropertyDefinition = {
@@ -101,7 +102,7 @@ function initProps (vm: Component, propsOptions: Object) {
101
102
function initData ( vm : Component ) {
102
103
let data = vm . $options . data
103
104
data = vm . _data = typeof data === 'function'
104
- ? data . call ( vm )
105
+ ? getData ( data , vm )
105
106
: data || { }
106
107
if ( ! isPlainObject ( data ) ) {
107
108
data = { }
@@ -130,6 +131,15 @@ function initData (vm: Component) {
130
131
observe ( data , true /* asRootData */ )
131
132
}
132
133
134
+ function getData ( data : Function , vm : Component ) : any {
135
+ try {
136
+ return data . call ( vm )
137
+ } catch ( e ) {
138
+ handleError ( e , vm , `data()` )
139
+ return { }
140
+ }
141
+ }
142
+
133
143
const computedWatcherOptions = { lazy : true }
134
144
135
145
function initComputed ( vm : Component , computed : Object ) {
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ describe('Error handling', () => {
7
7
// hooks that prevents the component from rendering, but should not
8
8
// break parent component
9
9
; [
10
+ [ 'data' , 'data()' ] ,
10
11
[ 'render' , 'render function' ] ,
11
12
[ 'beforeCreate' , 'beforeCreate hook' ] ,
12
13
[ 'created' , 'created hook' ] ,
@@ -128,6 +129,16 @@ describe('Error handling', () => {
128
129
function createErrorTestComponents ( ) {
129
130
const components = { }
130
131
132
+ // data
133
+ components . data = {
134
+ data ( ) {
135
+ throw new Error ( 'data' )
136
+ } ,
137
+ render ( h ) {
138
+ return h ( 'div' )
139
+ }
140
+ }
141
+
131
142
// render error
132
143
components . render = {
133
144
render ( h ) {
You can’t perform that action at this time.
0 commit comments