From: HcySunYang Date: Wed, 2 Sep 2020 01:05:51 +0000 (+0800) Subject: polish(teleport): do not warn missing target when teleport is disabled (#2021) X-Git-Tag: v3.0.0-rc.10~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93b8ff94a46aaab3309a52e9d0d17ab175afc920;p=thirdparty%2Fvuejs%2Fcore.git polish(teleport): do not warn missing target when teleport is disabled (#2021) --- diff --git a/packages/runtime-core/__tests__/components/Teleport.spec.ts b/packages/runtime-core/__tests__/components/Teleport.spec.ts index 44a08c6687..eb8ee41635 100644 --- a/packages/runtime-core/__tests__/components/Teleport.spec.ts +++ b/packages/runtime-core/__tests__/components/Teleport.spec.ts @@ -179,6 +179,36 @@ describe('renderer: teleport', () => { ) }) + test('should work when using template ref as target', async () => { + const root = nodeOps.createElement('div') + const target = ref(null) + const disabled = ref(true) + + const App = { + setup() { + return () => + h(Fragment, [ + h('div', { ref: target }), + h( + Teleport, + { to: target.value, disabled: disabled.value }, + h('div', 'teleported') + ) + ]) + } + } + render(h(App), root) + expect(serializeInner(root)).toMatchInlineSnapshot( + `"
teleported
"` + ) + + disabled.value = false + await nextTick() + expect(serializeInner(root)).toMatchInlineSnapshot( + `"
teleported
"` + ) + }) + test('disabled', () => { const target = nodeOps.createElement('div') const root = nodeOps.createElement('div') diff --git a/packages/runtime-core/src/components/Teleport.ts b/packages/runtime-core/src/components/Teleport.ts index 1d6a996de0..dae83eea61 100644 --- a/packages/runtime-core/src/components/Teleport.ts +++ b/packages/runtime-core/src/components/Teleport.ts @@ -14,7 +14,7 @@ import { warn } from '../warning' export type TeleportVNode = VNode export interface TeleportProps { - to: string | RendererElement + to: string | RendererElement | null | undefined disabled?: boolean } @@ -50,7 +50,7 @@ const resolveTarget = ( return target as any } } else { - if (__DEV__ && !targetSelector) { + if (__DEV__ && !targetSelector && !isTeleportDisabled(props)) { warn(`Invalid Teleport target: ${targetSelector}`) } return targetSelector as any @@ -94,7 +94,7 @@ export const TeleportImpl = { const targetAnchor = (n2.targetAnchor = createText('')) if (target) { insert(targetAnchor, target) - } else if (__DEV__) { + } else if (__DEV__ && !disabled) { warn('Invalid Teleport target on mount:', target, `(${typeof target})`) }