diff --git a/src/router/modules/demo.ts b/src/router/modules/demo.ts index 1a1c614..cbac471 100644 --- a/src/router/modules/demo.ts +++ b/src/router/modules/demo.ts @@ -186,6 +186,10 @@ const titleArr = [ key: 'formChangeTrack', title: '表单敏感字段变更追踪标记', }, + { + key: 'lodashExamples', + title: 'Lodash 经典用法', + }, ]; // @/views/demo/**/*.vue diff --git a/src/views/demo/lodashExamples.vue b/src/views/demo/lodashExamples.vue new file mode 100644 index 0000000..acce815 --- /dev/null +++ b/src/views/demo/lodashExamples.vue @@ -0,0 +1,248 @@ + + + + + + + 1. 对象深度比较 (isEqual) + _.isEqual(obj1, obj2) + + + + + 对象 A + + + + 对象 B + + + + + 比较是否相等 + + 结果: + + {{ compareResult ? '完全一致' : '不一致' }} + + 待比较 + + + + + + + + + 2. 深拷贝 (cloneDeep) + _.cloneDeep(obj) + + + + 原始对象包含嵌套属性,修改副本不会影响原对象。 + + + 原始对象 (source) + {{ sourceObj }} + 修改原对象.info.age + 1 + + + 深拷贝副本 (cloned) + {{ clonedObj }} + 修改副本.info.age + 1 + + + + 操作提示:点击上方按钮修改属性,观察两个对象互不干扰。 + + + + + + + + + 3. 防抖与节流 (debounce & throttle) + + _.debounce + _.throttle + + + + + + + + 防抖 (Debounce) + 停止输入 500ms 后触发搜索 + + + 实际触发次数: {{ debounceCount }} + + 最后触发时间: {{ debounceTime }} + + + + + 节流 (Throttle) + 每 1000ms 最多触发一次点击 + + 疯狂点击我 (1s 响应一次) + + + 实际触发次数: {{ throttleCount }} + + 最后触发时间: {{ throttleTime }} + + + + + + + + + 4. 常用数组操作 + + _.uniq + _.shuffle + _.chunk + + + + + + + 数组去重 (_.uniq) + + 原数组: [1, 2, 2, 3, 1, 4] + + {{ uniqResult }} + + + + + + 数组乱序 (_.shuffle) + + 原数组: [1, 2, 3, 4, 5] + 点击打乱 + {{ shuffleResult }} + + + + + + 数组分块 (_.chunk) + 将 ['a', 'b', 'c', 'd', 'e'] 按大小 2 分块 + + {{ chunkResult }} + + + + + + + + + +
原始对象包含嵌套属性,修改副本不会影响原对象。
{{ sourceObj }}
{{ clonedObj }}
停止输入 500ms 后触发搜索
每 1000ms 最多触发一次点击
将 ['a', 'b', 'c', 'd', 'e'] 按大小 2 分块