Ver Fonte

fix(provide/inject): merge provide properly from mixins

close #6175
Evan You há 9 anos atrás
pai
commit
303655116f
2 ficheiros alterados com 42 adições e 1 exclusões
  1. 1 1
      src/core/util/options.js
  2. 41 0
      test/unit/features/options/inject.spec.js

+ 1 - 1
src/core/util/options.js

@@ -85,7 +85,7 @@ export function mergeDataOrFn (
     return function mergedDataFn () {
       return mergeData(
         typeof childVal === 'function' ? childVal.call(this) : childVal,
-        parentVal.call(this)
+        typeof parentVal === 'function' ? parentVal.call(this) : parentVal
       )
     }
   } else if (parentVal || childVal) {

+ 41 - 0
test/unit/features/options/inject.spec.js

@@ -382,6 +382,7 @@ describe('Options provide/inject', () => {
 
     expect(injected).toEqual(['foo', 'bar'])
   })
+
   it('should merge provide from mixins (functions)', () => {
     const mixinA = { provide: () => ({ foo: 'foo' }) }
     const mixinB = { provide: () => ({ bar: 'bar' }) }
@@ -401,6 +402,7 @@ describe('Options provide/inject', () => {
 
     expect(injected).toEqual(['foo', 'bar'])
   })
+
   it('should merge provide from mixins (mix of objects and functions)', () => {
     const mixinA = { provide: { foo: 'foo' }}
     const mixinB = { provide: () => ({ bar: 'bar' }) }
@@ -422,6 +424,7 @@ describe('Options provide/inject', () => {
 
     expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
   })
+
   it('should merge provide from mixins and override existing keys', () => {
     const mixinA = { provide: { foo: 'foo' }}
     const mixinB = { provide: { foo: 'bar' }}
@@ -441,6 +444,7 @@ describe('Options provide/inject', () => {
 
     expect(injected).toEqual(['bar'])
   })
+
   it('should merge provide when Vue.extend', () => {
     const mixinA = { provide: () => ({ foo: 'foo' }) }
     const child = {
@@ -504,4 +508,41 @@ describe('Options provide/inject', () => {
     expect(isObserver(child.bar)).toBe(false)
     expect(isObserver(child.baz)).toBe(false)
   })
+
+  // #6175
+  it('merge provide properly from mixins', () => {
+    const ProvideFooMixin = {
+      provide: {
+        foo: 'foo injected'
+      }
+    }
+
+    const ProvideBarMixin = {
+      provide: {
+        bar: 'bar injected'
+      }
+    }
+
+    const Child = {
+      inject: ['foo', 'bar'],
+      render (h) {
+        return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
+      }
+    }
+
+    const Parent = {
+      mixins: [ProvideFooMixin, ProvideBarMixin],
+      render (h) {
+        return h(Child)
+      }
+    }
+
+    const vm = new Vue({
+      render (h) {
+        return h(Parent)
+      }
+    }).$mount()
+
+    expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
+  })
 })