首页 > 解决方案 > 更新 lodash 版本到 4.17.4 破解代码

问题描述

我将 lodash 更新到4.17.4版本,Typescript 开始抛出这个错误:

TypeError: _.uniqBy is not a function Uncaught TypeError: _.split is not a function

我的代码如下所示:

import * as _ from 'lodash';
const uniqueRecordType = _.uniqBy(rArr,'recordtype');

这个功能是否已从lodash中删除?

标签: node.jstypescriptexpresslodash

解决方案


根据文档,以下应该有效:

const uniqBy = require('lodash.uniqby');

const uniqueRecordType = uniqBy(rArr,'recordtype');

如何安装 lodash 依赖项:

npm install --save lodash.uniqby

当使用 ECMAScript 5 和 CommonJS 模块时,你可以像这样导入它:

var uniqBy = require('lodash.uniqby');

使用 ES6 模块,这将是:

import uniqBy from 'lodash.uniqBy';

推荐阅读