From: Evan You Date: Tue, 31 Mar 2020 14:30:12 +0000 (-0400) Subject: feat(templateRef): support template ref for all vnode types X-Git-Tag: v3.0.0-alpha.11~33 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=55b364decc903a6c7fccd1cdcdcfc79948c848a2;p=thirdparty%2Fvuejs%2Fcore.git feat(templateRef): support template ref for all vnode types --- diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index 5952abc734..0577f8c4a6 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -363,7 +363,7 @@ function baseCreateRenderer( n1 = null } - const { type, shapeFlag } = n2 + const { type, ref, shapeFlag } = n2 switch (type) { case Text: processText(n1, n2, container, anchor) @@ -439,6 +439,13 @@ function baseCreateRenderer( warn('Invalid VNode type:', type, `(${typeof type})`) } } + + // set ref + if (ref != null && parentComponent) { + const refValue = + shapeFlag & ShapeFlags.STATEFUL_COMPONENT ? n2.component!.proxy : n2.el + setRef(ref, n1 && n1.ref, parentComponent, refValue) + } } const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => { @@ -518,9 +525,6 @@ function baseCreateRenderer( } else { patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized) } - if (n2.ref != null && parentComponent) { - setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el) - } } const mountElement = ( @@ -1005,17 +1009,6 @@ function baseCreateRenderer( n2.el = n1.el } } - if (n2.ref != null && parentComponent) { - if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) { - pushWarningContext(n2) - warn( - `Functional components do not support "ref" because they do not ` + - `have instances.` - ) - popWarningContext() - } - setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.proxy) - } } const mountComponent: MountComponentFn = (