feat(Preview): 优化实例创建更新逻辑, 防止重复创建dom (#3979)

* perf(Preview): 优化实例创建更新逻辑, 防止重复创建dom

* perf: 逻辑优化
This commit is contained in:
苗大 2024-07-26 14:30:14 +08:00 committed by GitHub
parent 5b7b6b1780
commit 25699c0b60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,7 @@
import type { Options, Props } from './typing';
import ImgPreview from './Functional.vue';
import { isClient } from '@/utils/is';
import { createVNode, render } from 'vue';
import ImgPreview from './Functional.vue';
import type { Options, Props } from './typing';
let instance: ReturnType<typeof createVNode> | null = null;
export function createImgPreview(options: Options) {
@ -10,8 +10,13 @@ export function createImgPreview(options: Options) {
const container = document.createElement('div');
Object.assign(propsData, { show: true, index: 0, scaleStep: 100 }, options);
instance = createVNode(ImgPreview, propsData);
render(instance, container);
document.body.appendChild(container);
if (instance?.component) {
// 存在实例时更新props
Object.assign(instance.component.props, propsData);
} else {
instance = createVNode(ImgPreview, propsData);
render(instance, container);
document.body.appendChild(container);
}
return instance.component?.exposed;
}