未找到 Angular 2/Angular 2 CLI/lodash 函数

标签 angular lodash angular2-cli

我们在 angular-cli beta 18 中遇到错误

Error: Uncaught (in promise): TypeError: __WEBPACK_IMPORTED_MODULE_6_lodash__.find is not a function

Lodash + 类型已添加到 package.json:-

"lodash": "4.14",
"@types/lodash": "4.14.38",

在 angular-cli.json 的脚本部分中,我们有对该模块的引用

"../node_modules/lodash/lodash.js",

我们正在使用以下方式导入 lodash:-

从'lodash'导入* as _;

编译后的scripts.bundle.js包含lodash javascript。

有什么我忘记的事情吗?

谢谢!

最佳答案

编辑: 不要添加到 angular-cli.json 文件脚本部分。这是不需要的!

您似乎已经拥有它,只是为了确保,以下是我如何在我的项目之一中重现它的步骤:

  • 停止当前ng服务
  • 执行npm install lodash --savenpm install @types/lodash --save-dev
  • 在 Controller 中执行 import * as _ from 'lodash';
  • 像这样使用它:console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
  • 再次发球

关于未找到 Angular 2/Angular 2 CLI/lodash 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40379678/

相关文章:

lodash - 创建具有特定键的对象数组,然后从对象中删除该键

angular - 如何在 Angular 2 中使用窗口对象?

angular - 使用 firebase 函数和 nodemailer 从联系表单发送电子邮件

angular - 传递给 ngclass 枚举字符串值

Angular 6 : Guard on all routes is not working

node.js - 错误 : EPERM Unable to install @angular/cli

angular - Angular 2 提前编译中的未知编译器选项 'angularCompilerOptions'

angular - 跟踪滚动位置并通知其他组件

javascript - Lodash集合批量更新

javascript - 如果另一个数组具有相同的元素,则连接数组元素