瀏覽代碼

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

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

* perf: 逻辑优化
苗大 8 月之前
父節點
當前提交
25699c0b60
共有 1 個文件被更改,包括 10 次插入5 次删除
  1. 10 5
      src/components/Preview/src/functional.ts

+ 10 - 5
src/components/Preview/src/functional.ts

@@ -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;
 }