feat(Preview): 优化实例创建更新逻辑, 防止重复创建dom (#3979)
* perf(Preview): 优化实例创建更新逻辑, 防止重复创建dom * perf: 逻辑优化
This commit is contained in:
parent
5b7b6b1780
commit
25699c0b60
|
|
@ -1,7 +1,7 @@
|
||||||
import type { Options, Props } from './typing';
|
|
||||||
import ImgPreview from './Functional.vue';
|
|
||||||
import { isClient } from '@/utils/is';
|
import { isClient } from '@/utils/is';
|
||||||
import { createVNode, render } from 'vue';
|
import { createVNode, render } from 'vue';
|
||||||
|
import ImgPreview from './Functional.vue';
|
||||||
|
import type { Options, Props } from './typing';
|
||||||
|
|
||||||
let instance: ReturnType<typeof createVNode> | null = null;
|
let instance: ReturnType<typeof createVNode> | null = null;
|
||||||
export function createImgPreview(options: Options) {
|
export function createImgPreview(options: Options) {
|
||||||
|
|
@ -10,8 +10,13 @@ export function createImgPreview(options: Options) {
|
||||||
const container = document.createElement('div');
|
const container = document.createElement('div');
|
||||||
Object.assign(propsData, { show: true, index: 0, scaleStep: 100 }, options);
|
Object.assign(propsData, { show: true, index: 0, scaleStep: 100 }, options);
|
||||||
|
|
||||||
|
if (instance?.component) {
|
||||||
|
// 存在实例时,更新props
|
||||||
|
Object.assign(instance.component.props, propsData);
|
||||||
|
} else {
|
||||||
instance = createVNode(ImgPreview, propsData);
|
instance = createVNode(ImgPreview, propsData);
|
||||||
render(instance, container);
|
render(instance, container);
|
||||||
document.body.appendChild(container);
|
document.body.appendChild(container);
|
||||||
|
}
|
||||||
return instance.component?.exposed;
|
return instance.component?.exposed;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue