fix(template-ref): fix string template refs inside slots

This commit is contained in:
Evan You
2020-02-25 18:29:51 -05:00
parent 8cb0b83088
commit 3eab143843
6 changed files with 110 additions and 7 deletions

View File

@@ -30,7 +30,8 @@ import {
isFunction,
PatchFlags,
ShapeFlags,
NOOP
NOOP,
isArray
} from '@vue/shared'
import {
queueJob,
@@ -1793,11 +1794,19 @@ function baseCreateRenderer<
}
const setRef = (
ref: string | Function | Ref,
oldRef: string | Function | Ref | null,
ref: string | Function | Ref | [ComponentPublicInstance, string],
oldRef: string | Function | Ref | [ComponentPublicInstance, string] | null,
parent: ComponentInternalInstance,
value: HostNode | ComponentPublicInstance | null
) => {
if (isArray(ref)) {
// template string refs are compiled into tuples like [ctx, key] to
// ensure refs inside slots are set on the correct owner instance.
const [{ $: owner }, key] = ref
setRef(key, oldRef && (oldRef as any[])[1], owner, value)
return
}
const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
const renderContext = toRaw(parent.renderContext)
@@ -1823,7 +1832,7 @@ function baseCreateRenderer<
} else if (isRef(ref)) {
ref.value = value
} else if (isFunction(ref)) {
callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value])
} else if (__DEV__) {
warn('Invalid template ref type:', value, `(${typeof value})`)
}