fix(runtime-core/inject): handle optional from option in inject object config (#2073)

This commit is contained in:
Stanislav Lashmanov 2020-09-14 19:52:19 +03:00 committed by GitHub
parent a096a58e41
commit 313dd06065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 6 deletions

View File

@ -253,7 +253,16 @@ describe('api: options', () => {
} }
}, },
render() { render() {
return [h(ChildA), h(ChildB), h(ChildC), h(ChildD), h(ChildE)] return [
h(ChildA),
h(ChildB),
h(ChildC),
h(ChildD),
h(ChildE),
h(ChildF),
h(ChildG),
h(ChildH)
]
} }
}) })
@ -272,19 +281,37 @@ describe('api: options', () => {
from: 'a' from: 'a'
} }
}) })
const ChildD = defineChild({ const ChildD = defineChild(
{
a: {
default: () => 0
}
},
'a'
)
const ChildE = defineChild({
b: { b: {
from: 'c', from: 'c',
default: 2 default: 2
} }
}) })
const ChildE = defineChild({ const ChildF = defineChild({
b: { b: {
from: 'c', from: 'c',
default: () => 3 default: () => 3
} }
}) })
expect(renderToString(h(Root))).toBe(`11123`) const ChildG = defineChild({
b: {
default: 4
}
})
const ChildH = defineChild({
b: {
default: () => 5
}
})
expect(renderToString(h(Root))).toBe(`11112345`)
}) })
test('lifecycle', async () => { test('lifecycle', async () => {

View File

@ -275,7 +275,7 @@ type ComponentInjectOptions =
| string[] | string[]
| Record< | Record<
string | symbol, string | symbol,
string | symbol | { from: string | symbol; default?: unknown } string | symbol | { from?: string | symbol; default?: unknown }
> >
interface LegacyOptions< interface LegacyOptions<
@ -460,7 +460,7 @@ export function applyOptions(
const opt = injectOptions[key] const opt = injectOptions[key]
if (isObject(opt)) { if (isObject(opt)) {
ctx[key] = inject( ctx[key] = inject(
opt.from, opt.from || key,
opt.default, opt.default,
true /* treat default function as factory */ true /* treat default function as factory */
) )