fix(runtime-core): bind default function of inject to instance (#3925)
fix #3923
This commit is contained in:
		
							parent
							
								
									bc100c5c48
								
							
						
					
					
						commit
						db1dc1c630
					
				@ -7,7 +7,8 @@ import {
 | 
				
			|||||||
  nextTick,
 | 
					  nextTick,
 | 
				
			||||||
  Ref,
 | 
					  Ref,
 | 
				
			||||||
  readonly,
 | 
					  readonly,
 | 
				
			||||||
  reactive
 | 
					  reactive,
 | 
				
			||||||
 | 
					  defineComponent
 | 
				
			||||||
} from '../src/index'
 | 
					} from '../src/index'
 | 
				
			||||||
import { render, nodeOps, serialize } from '@vue/runtime-test'
 | 
					import { render, nodeOps, serialize } from '@vue/runtime-test'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -91,6 +92,34 @@ describe('api: provide/inject', () => {
 | 
				
			|||||||
    expect(serialize(root)).toBe(`<div>foobar</div>`)
 | 
					    expect(serialize(root)).toBe(`<div>foobar</div>`)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('bound to instance', () => {
 | 
				
			||||||
 | 
					    const Provider = {
 | 
				
			||||||
 | 
					      setup() {
 | 
				
			||||||
 | 
					        return () => h(Consumer)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const Consumer = defineComponent({
 | 
				
			||||||
 | 
					      name: 'Consumer',
 | 
				
			||||||
 | 
					      inject: {
 | 
				
			||||||
 | 
					        foo: {
 | 
				
			||||||
 | 
					          from: 'foo',
 | 
				
			||||||
 | 
					          default() {
 | 
				
			||||||
 | 
					            return this!.$options.name
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      render() {
 | 
				
			||||||
 | 
					        // @ts-ignore
 | 
				
			||||||
 | 
					        return this.foo
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const root = nodeOps.createElement('div')
 | 
				
			||||||
 | 
					    render(h(Provider), root)
 | 
				
			||||||
 | 
					    expect(serialize(root)).toBe(`<div>Consumer</div>`)
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('nested providers', () => {
 | 
					  it('nested providers', () => {
 | 
				
			||||||
    const ProviderOne = {
 | 
					    const ProviderOne = {
 | 
				
			||||||
      setup() {
 | 
					      setup() {
 | 
				
			||||||
 | 
				
			|||||||
@ -60,7 +60,7 @@ export function inject(
 | 
				
			|||||||
      return provides[key as string]
 | 
					      return provides[key as string]
 | 
				
			||||||
    } else if (arguments.length > 1) {
 | 
					    } else if (arguments.length > 1) {
 | 
				
			||||||
      return treatDefaultAsFactory && isFunction(defaultValue)
 | 
					      return treatDefaultAsFactory && isFunction(defaultValue)
 | 
				
			||||||
        ? defaultValue()
 | 
					        ? defaultValue.call(instance.proxy)
 | 
				
			||||||
        : defaultValue
 | 
					        : defaultValue
 | 
				
			||||||
    } else if (__DEV__) {
 | 
					    } else if (__DEV__) {
 | 
				
			||||||
      warn(`injection "${String(key)}" not found.`)
 | 
					      warn(`injection "${String(key)}" not found.`)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user