Просмотр исходного кода

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

#6739
wenlu.wang 8 лет назад
Родитель
Сommit
3a5432a9e3
2 измененных файлов с 18 добавлено и 1 удалено
  1. 1 1
      src/core/instance/state.js
  2. 17 0
      test/unit/features/options/data.spec.js

+ 1 - 1
src/core/instance/state.js

@@ -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 {}

+ 17 - 0
test/unit/features/options/data.spec.js

@@ -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>')
+  })
 })