feat(core): call data method with this value (#6760)

#6739
This commit is contained in:
wenlu.wang 2017-10-10 08:39:30 -05:00 committed by Evan You
parent 596257cf6f
commit 3a5432a9e3
2 changed files with 18 additions and 1 deletions

View File

@ -161,7 +161,7 @@ function initData (vm: Component) {
function getData (data: Function, vm: Component): any {
try {
return data.call(vm)
return data.call(vm, vm)
} catch (e) {
handleError(e, vm, `data()`)
return {}

View File

@ -106,4 +106,21 @@ describe('Options data', () => {
})
expect(vm.a).toBe(1)
})
it('should called with this', () => {
const vm = new Vue({
template: '<div><child></child></div>',
provide: { foo: 1 },
components: {
child: {
template: '<span>{{bar}}</span>',
inject: ['foo'],
data ({ foo }) {
return { bar: 'foo:' + foo }
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>foo:1</span>')
})
})