Skip to content

Commit

Permalink
fix(runtime-core): bind default function of inject to instance (#3925)
Browse files Browse the repository at this point in the history
fix #3923
  • Loading branch information
eunjae-lee authored Jun 9, 2021
1 parent bc100c5 commit db1dc1c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion packages/runtime-core/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
nextTick,
Ref,
readonly,
reactive
reactive,
defineComponent
} from '../src/index'
import { render, nodeOps, serialize } from '@vue/runtime-test'

Expand Down Expand Up @@ -91,6 +92,34 @@ describe('api: provide/inject', () => {
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', () => {
const ProviderOne = {
setup() {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function inject(
return provides[key as string]
} else if (arguments.length > 1) {
return treatDefaultAsFactory && isFunction(defaultValue)
? defaultValue()
? defaultValue.call(instance.proxy)
: defaultValue
} else if (__DEV__) {
warn(`injection "${String(key)}" not found.`)
Expand Down

0 comments on commit db1dc1c

Please sign in to comment.