Skip to content

Commit

Permalink
feat(core): call data method with this value (vuejs#6760)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl authored and hefeng committed Jan 25, 2019
1 parent e13b4ca commit 4506950
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/features/options/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>')
})
})

0 comments on commit 4506950

Please sign in to comment.