Skip to content

Instantly share code, notes, and snippets.

@lisonge
Last active November 14, 2025 07:13
Show Gist options
  • Select an option

  • Save lisonge/28887640a8147f5a6cb22722acda4aea to your computer and use it in GitHub Desktop.

Select an option

Save lisonge/28887640a8147f5a6cb22722acda4aea to your computer and use it in GitHub Desktop.
import { ElDialog } from 'element-plus'
/**
* 解决 ElDialog 不显示时仍然渲染内容的问题
*/
const PerfDialog = defineComponent(
(props, ctx) => {
const render = shallowRef(props.modelValue)
const onClosed = async () => {
await nextTick()
await new Promise((r) => setTimeout(r))
// 如果是内部触发关闭:emit closed 之后才会 emit update:modelValue false
if (!props.modelValue) {
render.value = false
}
}
watch(
() => props.modelValue,
() => {
if (props.modelValue) {
render.value = true
}
}
)
const emitProps: any = Object.fromEntries(
Object.keys(ElDialog.emits!).map((name) => {
return [
'on' + name.charAt(0).toUpperCase() + name.substring(1),
(e: any) => {
if (name === 'closed') {
onClosed()
}
ctx.emit(name as any, e)
}
]
})
)
return () => {
if (render.value) {
return h(
ElDialog,
{
...emitProps,
...props,
...ctx.attrs
},
ctx.slots
)
}
}
},
Object.fromEntries(Object.entries(ElDialog).filter(([k]) => ['props', 'emits', 'name', 'inheritAttrs'].includes(k)))
) as typeof ElDialog
export default PerfDialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment