当前位置: 首页 > article >正文

Vue3-TS-Lodash:理解Lodash / 常用方法积累

一、Lodash官网

Lodash 简介 | Lodash中文文档 | Lodash中文网

二、理解Lodash

Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库。它提供了大量的函数来帮助你处理数组、数值、对象、字符串等,使你的代码更加简洁、易读和高效。Lodash 的设计哲学是让你能够使用函数式编程的方法来操作数据,同时避免修改原始数据(除非明确指定)。

三、Lodash具备哪些能力

序号能力解释
1数组 / Array如过滤、映射、排序、分块、去重、查找、比较等
2集合 / Collection如并集、交集、差集、子集、筛选等
3函数如柯里化、防抖、节流、合成、条件判断等;
4语言提供了一系列用于检查类型、验证值等的函数
5数学如求和、平均数、最大值、最小值、随机数等;
6数字clamp(number, [lower], upper)返回限制在 lower 和 upper 之间的值。
inRange(number, [start=0], end)检查 n 是否在 start 与 end 之间
random([lower=0], [upper=1], [floating])产生一个包括 lower 与 upper 之间的数
7对象如深拷贝、合并、遍历、转换、比较等
8SeqSeq 模块(或称为序列)提供了一种链式调用的方式来处理数据,同时支持惰性计算(Lazy Evaluation),这意味着只有在需要最终结果时,才会执行链中的操作。这种机制可以显著提高处理大数据集时的性能,因为它允许在数据实际被使用之前避免不必要的计算。
9字符串如切割、重复、填充、大小写转换、字符串匹配、转义等;
10实用函数
11Properties
12Methods
Lodash虽然提供了很多方法,但并不意味着在实际开发中一定要全部使用。在使用Lodash时,应根据实际需要选取合适的方法,以提高代码的可读性和执行效率。

四、vue3 ts 项目安装

4.1、安装

pnpm install lodash
pnpm install @types/lodash

4.2、全局引入

在项目的入口文件(main.ts)中全局引入Lodash,并将其挂载。
但请注意,这种方式虽然方便,但可能会增加项目的打包体积,因为Lodash是一个相对较大的库。

main.ts

import _ from 'lodash' 
app.config.globalProperties.$lodash = _ 
import { createApp, getCurrentInstance } from 'vue'
import './style.css'
import '@v3/assets/styles/tailwind.css'
import '@v3/assets/styles/reset.css'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import router from './routers/index'
import { api } from './api/index'
import 'vue-global-api'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import _ from 'lodash' 

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
const app = createApp(App)
app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(pinia)
app.use(router)
app.config.globalProperties.$api = api
app.config.globalProperties.$lodash = _ 
app.mount('#app')

页面使用

<script setup lang="ts">
let Lodash = internalInstance && internalInstance.appContext.config.globalProperties.$lodash
getUser(params).then((res: any)=>{
    console.log('lodash', Lodash.hasIn(res, ['payload']))
    // true
}
</script>

感觉比较麻烦,后边找找看简便方法

出于性能和打包体积的考虑,通常不推荐全局引入Lodash。更好的做法是按需引入。 

4.3、按需引入

在组件或文件中,根据需要引入Lodash的特定函数。这样做的好处是可以只引入你需要的函数,从而减小项目的打包体积。

<script setup lang="ts">  
import { defineComponent } from 'vue'  
import { debounce } from 'lodash'  
  
// 使用debounce函数  
const handleInput = debounce((value: string) => {  
   console.log(value)  
}, 500)  
</script>  
  
<template>  
  <input type="text" @input="handleInput($event.target.value)" />  
</template>

五、配置TypeScript以支持Lodash

由于你已经安装了@types/lodash,TypeScript应该能够自动识别Lodash的类型定义。但是,如果你在使用TypeScript时遇到了与Lodash相关的类型错误或警告,请确保你的tsconfig.json文件配置正确。特别是,如果你的项目中使用了ES模块导入方式,并且遇到了默认导入的警告,你可能需要在tsconfig.json中添加以下配置

{  
  "compilerOptions": {  
    "allowSyntheticDefaultImports": true,  
    "esModuleInterop": true  
  }  
}

六、常用方法

6.1、hasIn

let result = hasIn(res, ['payload']) ? res.payload : []

对象路径正确 返回 true ,否则 false

<script setup lang="ts">
import { hasIn } from 'lodash'
getUser(params).then((res: any)=>{
    console.log('lodash-hasIn', hasIn(res, ['payload']))
    // true 测试成功
}
</script>

6.2、待补充

更多内容待补充

参考链接

lodash.hasIn | Lodash中文文档 | Lodash中文网


http://www.kler.cn/news/336103.html

相关文章:

  • 银行数字化转型导师坚鹏:2025年银行开门红绩效管理方法及案例
  • 家庭厨房的蟑螂消灭治理方法
  • 【机器学习-无监督学习】概率图模型
  • 毕业设计项目——基于transformer的中文医疗领域命名实体识别(论文/代码)
  • class 029 重要排序算法的总结
  • 数据分布过于集中 怎么办,python 人工智能 ,数据分析,机器学习pytorch tensorflow ,
  • 【C++】二叉搜索树+变身 = 红黑树
  • Python的几个高级特性
  • 谷粒商城のRabbitMQ基础篇
  • 制作离线版Koczkatamas工具包
  • 洛谷 P11045 [蓝桥杯 2024 省 Java B] 最优分组
  • Linux 系统 nvm 管理node无法使用
  • python爬虫 - 初识requests模块
  • 民锋金融:稳健发展的战略与未来展望
  • 代码随想录Day 62|Floyd 算法精讲、A \* 算法精讲 (A star算法),题目:97. 小明逛公园、127. 骑士的攻击
  • 关于BSV区块链覆盖网络的常见问题解答(下篇)
  • CRM如何和ERP融合,才能打破信息孤岛效应。
  • vite学习教程05、vite+vue2构建本地 SVG 图标
  • 基于SSM的出租车租赁管理系统的设计与实现
  • OpenGL ES 着色器(5)