【问题标题】:Updating lodash version to 4.17.4 break the code更新 lodash 版本到 4.17.4 破解代码
【发布时间】:2019-05-30 13:55:27
【问题描述】:

我将 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 中删除?

【问题讨论】:

  • 请在您尝试使用这两个函数的位置显示您的 lodash 导入语句和代码。
  • 如果有人在 Typescript 中遇到这些错误 - 您可能需要进行很多更改。

标签: node.js typescript express lodash


【解决方案1】:

Lodash 版本 4.17.4导入为:

import _ from 'lodash';

这是我对 stackblitz 的测试,lodash 版本为 4.17.4 https://stackblitz.com/edit/typescript-54031396?file=index.ts

当前的 Lodash 版本是 4.17.11,它将与

一起使用
import * as _ from 'lodash';

所以你可以升级到 4.17.11 或者如果你想使用版本 4.17.4,你可以将 import 更改为 import _ from 'lodash';

希望对你有所帮助:P

【讨论】:

  • import _ from lodash 抛出错误 - 模块 '"/Users/pritisolanki/rri-core-api/node_modules/@types/lodash/index"' 没有默认 export.ts(1192)跨度>
  • lodash 应该作为模块导入,而不是作为所有内容引用。
  • 我试过stackblitz,如果你的版本是4.17.4,它就可以工作。你确定你的 lodash 版本是 4.17.4
【解决方案2】:

根据文档,以下应该可以工作:

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';

【讨论】:

  • 我认为这不是解决他问题的最佳方法,因为他使用的是lodash
  • 为什么这不是解决他的问题的最佳方式?上面的例子是考虑到 lodash。
猜你喜欢
  • 2020-09-21
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多