mirror of https://github.com/vuejs/vue.git
also catch error in data() (close #5198)
This commit is contained in:
parent
26f196780c
commit
59a372229b
|
@ -7,18 +7,19 @@ import {
|
||||||
set,
|
set,
|
||||||
del,
|
del,
|
||||||
observe,
|
observe,
|
||||||
defineReactive,
|
observerState,
|
||||||
observerState
|
defineReactive
|
||||||
} from '../observer/index'
|
} from '../observer/index'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
warn,
|
warn,
|
||||||
|
bind,
|
||||||
|
noop,
|
||||||
hasOwn,
|
hasOwn,
|
||||||
isReserved,
|
isReserved,
|
||||||
isPlainObject,
|
handleError,
|
||||||
bind,
|
|
||||||
validateProp,
|
validateProp,
|
||||||
noop
|
isPlainObject
|
||||||
} from '../util/index'
|
} from '../util/index'
|
||||||
|
|
||||||
const sharedPropertyDefinition = {
|
const sharedPropertyDefinition = {
|
||||||
|
@ -101,7 +102,7 @@ function initProps (vm: Component, propsOptions: Object) {
|
||||||
function initData (vm: Component) {
|
function initData (vm: Component) {
|
||||||
let data = vm.$options.data
|
let data = vm.$options.data
|
||||||
data = vm._data = typeof data === 'function'
|
data = vm._data = typeof data === 'function'
|
||||||
? data.call(vm)
|
? getData(data, vm)
|
||||||
: data || {}
|
: data || {}
|
||||||
if (!isPlainObject(data)) {
|
if (!isPlainObject(data)) {
|
||||||
data = {}
|
data = {}
|
||||||
|
@ -130,6 +131,15 @@ function initData (vm: Component) {
|
||||||
observe(data, true /* asRootData */)
|
observe(data, true /* asRootData */)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getData (data: Function, vm: Component): any {
|
||||||
|
try {
|
||||||
|
return data.call(vm)
|
||||||
|
} catch (e) {
|
||||||
|
handleError(e, vm, `data()`)
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const computedWatcherOptions = { lazy: true }
|
const computedWatcherOptions = { lazy: true }
|
||||||
|
|
||||||
function initComputed (vm: Component, computed: Object) {
|
function initComputed (vm: Component, computed: Object) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ describe('Error handling', () => {
|
||||||
// hooks that prevents the component from rendering, but should not
|
// hooks that prevents the component from rendering, but should not
|
||||||
// break parent component
|
// break parent component
|
||||||
;[
|
;[
|
||||||
|
['data', 'data()'],
|
||||||
['render', 'render function'],
|
['render', 'render function'],
|
||||||
['beforeCreate', 'beforeCreate hook'],
|
['beforeCreate', 'beforeCreate hook'],
|
||||||
['created', 'created hook'],
|
['created', 'created hook'],
|
||||||
|
@ -128,6 +129,16 @@ describe('Error handling', () => {
|
||||||
function createErrorTestComponents () {
|
function createErrorTestComponents () {
|
||||||
const components = {}
|
const components = {}
|
||||||
|
|
||||||
|
// data
|
||||||
|
components.data = {
|
||||||
|
data () {
|
||||||
|
throw new Error('data')
|
||||||
|
},
|
||||||
|
render (h) {
|
||||||
|
return h('div')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// render error
|
// render error
|
||||||
components.render = {
|
components.render = {
|
||||||
render (h) {
|
render (h) {
|
||||||
|
|
Loading…
Reference in New Issue