parent
6dbc6c4cd0
commit
d4724619fc
@ -241,7 +241,7 @@ describe('api: options', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('provide/inject', () => {
|
test('provide/inject', () => {
|
||||||
const Root = {
|
const Root = defineComponent({
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
a: 1
|
a: 1
|
||||||
@ -253,45 +253,38 @@ describe('api: options', () => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
render() {
|
render() {
|
||||||
return [h(ChildA), h(ChildB), h(ChildC), h(ChildD)]
|
return [h(ChildA), h(ChildB), h(ChildC), h(ChildD), h(ChildE)]
|
||||||
}
|
}
|
||||||
} as any
|
})
|
||||||
const ChildA = {
|
|
||||||
inject: ['a'],
|
const defineChild = (injectOptions: any, injectedKey = 'b') =>
|
||||||
|
({
|
||||||
|
inject: injectOptions,
|
||||||
render() {
|
render() {
|
||||||
return this.a
|
return this[injectedKey]
|
||||||
}
|
}
|
||||||
} as any
|
} as any)
|
||||||
const ChildB = {
|
|
||||||
// object alias
|
const ChildA = defineChild(['a'], 'a')
|
||||||
inject: { b: 'a' },
|
const ChildB = defineChild({ b: 'a' })
|
||||||
render() {
|
const ChildC = defineChild({
|
||||||
return this.b
|
|
||||||
}
|
|
||||||
} as any
|
|
||||||
const ChildC = {
|
|
||||||
inject: {
|
|
||||||
b: {
|
b: {
|
||||||
from: 'a'
|
from: 'a'
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
render() {
|
const ChildD = defineChild({
|
||||||
return this.b
|
|
||||||
}
|
|
||||||
} as any
|
|
||||||
const ChildD = {
|
|
||||||
inject: {
|
|
||||||
b: {
|
b: {
|
||||||
from: 'c',
|
from: 'c',
|
||||||
default: 2
|
default: 2
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
render() {
|
const ChildE = defineChild({
|
||||||
return this.b
|
b: {
|
||||||
|
from: 'c',
|
||||||
|
default: () => 3
|
||||||
}
|
}
|
||||||
} as any
|
})
|
||||||
|
expect(renderToString(h(Root))).toBe(`11123`)
|
||||||
expect(renderToString(h(Root))).toBe(`1112`)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('lifecycle', async () => {
|
test('lifecycle', async () => {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { isFunction } from '@vue/shared'
|
||||||
import { currentInstance } from './component'
|
import { currentInstance } from './component'
|
||||||
import { currentRenderingInstance } from './componentRenderUtils'
|
import { currentRenderingInstance } from './componentRenderUtils'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
@ -27,10 +28,15 @@ export function provide<T>(key: InjectionKey<T> | string, value: T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function inject<T>(key: InjectionKey<T> | string): T | undefined
|
export function inject<T>(key: InjectionKey<T> | string): T | undefined
|
||||||
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
|
export function inject<T>(
|
||||||
|
key: InjectionKey<T> | string,
|
||||||
|
defaultValue: T,
|
||||||
|
treatDefaultAsFactory?: boolean
|
||||||
|
): T
|
||||||
export function inject(
|
export function inject(
|
||||||
key: InjectionKey<any> | string,
|
key: InjectionKey<any> | string,
|
||||||
defaultValue?: unknown
|
defaultValue?: unknown,
|
||||||
|
treatDefaultAsFactory = false
|
||||||
) {
|
) {
|
||||||
// fallback to `currentRenderingInstance` so that this can be called in
|
// fallback to `currentRenderingInstance` so that this can be called in
|
||||||
// a functional component
|
// a functional component
|
||||||
@ -41,7 +47,9 @@ export function inject(
|
|||||||
// TS doesn't allow symbol as index type
|
// TS doesn't allow symbol as index type
|
||||||
return provides[key as string]
|
return provides[key as string]
|
||||||
} else if (arguments.length > 1) {
|
} else if (arguments.length > 1) {
|
||||||
return defaultValue
|
return treatDefaultAsFactory && isFunction(defaultValue)
|
||||||
|
? defaultValue()
|
||||||
|
: defaultValue
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
warn(`injection "${String(key)}" not found.`)
|
warn(`injection "${String(key)}" not found.`)
|
||||||
}
|
}
|
||||||
|
@ -457,7 +457,11 @@ export function applyOptions(
|
|||||||
for (const key in injectOptions) {
|
for (const key in injectOptions) {
|
||||||
const opt = injectOptions[key]
|
const opt = injectOptions[key]
|
||||||
if (isObject(opt)) {
|
if (isObject(opt)) {
|
||||||
ctx[key] = inject(opt.from, opt.default)
|
ctx[key] = inject(
|
||||||
|
opt.from,
|
||||||
|
opt.default,
|
||||||
|
true /* treat default function as factory */
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
ctx[key] = inject(opt)
|
ctx[key] = inject(opt)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user