Skip to content

Commit

Permalink
feat(core): call data method with this value (#6760)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl authored and yyx990803 committed Oct 10, 2017
1 parent 596257c commit 3a5432a
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 3a5432a

Please sign in to comment.