angular - 错误 TS1192:模块 @types/lodash/index 没有默认导出
问题描述
我Angular 6.x.x
在我的项目中使用以下lodash
依赖项。
"dependencies": {
"@angular/common": "6.0.9",
"@angular/core": "6.0.9",
...
"@types/lodash": "^4.14.114",
...
},
以下两个导入都不适合我。
import _ from 'lodash';
import * as _ from 'lodash';
解决方案
简短的
解决方案是:
- 添加
{"esModuleInterop": true}
到您的.tsconfig
- 确保您已安装
"@types/lodash"
在您的devDependencies
推理
简而言之:
与 ES6 默认相比, Typescript 具有不同的默认import
机制。
例如,您可以通过以下方式从模块中导入函数:
整体导入:
import _ from 'lodash'; const results = _.uniq([1, 1, 2, 3]);
只导入一部分:
import {uniq} from 'lodash'; const results = _.uniq([1, 1, 2, 3]);
但是,在 Typescript 中,它为您提供了一些语法糖2. Import a part
,如下所示:
// This is valid in Typescript
import uniq from 'lodash';
const results = _.uniq([1, 1, 2, 3]);
这很好,但1 Import as a whole
无法推断。通过添加esModuleInterop
它可以使默认的 ES6 行为再次起作用。
在另一个线程中提到了更多细节: Is there a way to use --esModuleInterop in tsconfig 而不是一个标志?
推荐阅读
- python - 有没有办法在 django 中添加模型值?
- python-3.x - 我正在尝试按列在列表中添加列表。有没有办法将它们与列表中缺少的变量相加?
- kotlin - 应用插件请求时发生异常[id:'org.jetbrains.kotlin.jvm', version:'1.3.40-eap-40']
- excel - 如何删除一行而不是隐藏它?
- file - Talend - 将动态模式与 CSV 文件一起使用时出现问题
- java - Java中的事件订阅模式
- webview - iOS webview 中的低延迟视频流有哪些选项?
- amazon-s3 - 如何在亚马逊 s3 中直接使用 cffile 上传?
- puppet - 解压特定目录中的文件
- python - 我无法设置 PNG 图像的像素数据