Browse Source

Revert "fix(deepMerge): fix recursive merge data without removing duplicate bugs (#2831)" (#2844)

This reverts commit 7ca007ecd510379d7c1b1fc7be98e83074371dca.

Co-authored-by: jinmao88 <50581550+jinmao88@users.noreply.github.com>
Kirk Lin 1 year ago
parent
commit
9c43c74131
1 changed files with 10 additions and 1 deletions
  1. 10 1
      src/utils/index.ts

+ 10 - 1
src/utils/index.ts

@@ -1,5 +1,5 @@
-import type { App, Component } from 'vue';
 import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
+import type { App, Component } from 'vue';
 
 import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es';
 import { unref } from 'vue';
@@ -52,6 +52,14 @@ export function deepMerge<T extends object | null | undefined, U extends object
   target: U,
   mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace',
 ): T & U {
+
+  return mergeWith(cloneDeep(target), source, (objValue, srcValue) => {
+    if (isObject(objValue) && isObject(srcValue)) {
+      return mergeWith(cloneDeep(objValue), srcValue, (prevValue, nextValue) => {
+        // 如果是数组,合并数组(去重) If it is an array, merge the array (remove duplicates)
+        return isArray(prevValue) ? unionWith(prevValue, nextValue, isEqual) : undefined;
+      });
+
   if (!target) {
     return source as T & U;
   }
@@ -70,6 +78,7 @@ export function deepMerge<T extends object | null | undefined, U extends object
         return source as T & U;
       default:
         throw new Error(`Unknown merge array strategy: ${mergeArrays}`);
+
     }
   }
   if (isObject(target) && isObject(source)) {