mirror of https://github.com/vuejs/core.git
feat(runtime-vapor): add v-once support to createDynamicComponent
This commit is contained in:
parent
9087cdf3cb
commit
0d81c0b854
|
@ -1,4 +1,4 @@
|
||||||
import { shallowRef } from '@vue/reactivity'
|
import { ref, shallowRef } from '@vue/reactivity'
|
||||||
import { nextTick } from '@vue/runtime-dom'
|
import { nextTick } from '@vue/runtime-dom'
|
||||||
import { createDynamicComponent } from '../src'
|
import { createDynamicComponent } from '../src'
|
||||||
import { makeRender } from './_utils'
|
import { makeRender } from './_utils'
|
||||||
|
@ -54,4 +54,42 @@ describe('api: createDynamicComponent', () => {
|
||||||
await nextTick()
|
await nextTick()
|
||||||
expect(html()).toBe('<baz></baz><!--dynamic-component-->')
|
expect(html()).toBe('<baz></baz><!--dynamic-component-->')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('with v-once', async () => {
|
||||||
|
const val = shallowRef<any>(A)
|
||||||
|
|
||||||
|
const { html } = define({
|
||||||
|
setup() {
|
||||||
|
return createDynamicComponent(() => val.value, null, null, true, true)
|
||||||
|
},
|
||||||
|
}).render()
|
||||||
|
|
||||||
|
expect(html()).toBe('AAA<!--dynamic-component-->')
|
||||||
|
|
||||||
|
val.value = B
|
||||||
|
await nextTick()
|
||||||
|
expect(html()).toBe('AAA<!--dynamic-component-->') // still AAA
|
||||||
|
})
|
||||||
|
|
||||||
|
test('fallback with v-once', async () => {
|
||||||
|
const val = shallowRef<any>('button')
|
||||||
|
const id = ref(0)
|
||||||
|
const { html } = define({
|
||||||
|
setup() {
|
||||||
|
return createDynamicComponent(
|
||||||
|
() => val.value,
|
||||||
|
{ id: () => id.value },
|
||||||
|
null,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}).render()
|
||||||
|
|
||||||
|
expect(html()).toBe('<button id="0"></button><!--dynamic-component-->')
|
||||||
|
|
||||||
|
id.value++
|
||||||
|
await nextTick()
|
||||||
|
expect(html()).toBe('<button id="0"></button><!--dynamic-component-->')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -15,7 +15,8 @@ export function createDynamicComponent(
|
||||||
const frag = __DEV__
|
const frag = __DEV__
|
||||||
? new DynamicFragment('dynamic-component')
|
? new DynamicFragment('dynamic-component')
|
||||||
: new DynamicFragment()
|
: new DynamicFragment()
|
||||||
renderEffect(() => {
|
|
||||||
|
const renderFn = () => {
|
||||||
const value = getter()
|
const value = getter()
|
||||||
frag.update(
|
frag.update(
|
||||||
() =>
|
() =>
|
||||||
|
@ -28,6 +29,10 @@ export function createDynamicComponent(
|
||||||
),
|
),
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if (once) renderFn()
|
||||||
|
else renderEffect(renderFn)
|
||||||
|
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,9 +479,10 @@ export function createComponentWithFallback(
|
||||||
;(el as any).$root = isSingleRoot
|
;(el as any).$root = isSingleRoot
|
||||||
|
|
||||||
if (rawProps) {
|
if (rawProps) {
|
||||||
renderEffect(() => {
|
const setFn = () =>
|
||||||
setDynamicProps(el, [resolveDynamicProps(rawProps as RawProps)])
|
setDynamicProps(el, [resolveDynamicProps(rawProps as RawProps)])
|
||||||
})
|
if (once) setFn()
|
||||||
|
else renderEffect(setFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rawSlots) {
|
if (rawSlots) {
|
||||||
|
|
Loading…
Reference in New Issue