【问题标题】:error TS1192: Module @types/lodash/index has no default export错误 TS1192:模块 @types/lodash/index 没有默认导出
【发布时间】:2018-07-28 02:45:45
【问题描述】:

我在我的项目中使用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';

【问题讨论】:

  • 如果你这个 (import * as _ from 'lodash';) 你得到任何错误
  • 不仅 @types/lodash 您还必须将 lodash 添加到您的 package.json
  • 我的回答有帮助吗?你的问题解决了吗?

标签: angular lodash


【解决方案1】:

似乎以下导入工作正常。我的环境有问题。删除node_modules目录并安装后,它工作了。

import * as _ from 'lodash';

【讨论】:

  • 你太棒了!
【解决方案2】:

简介

解决办法是:

  1. {"esModuleInterop": true} 添加到您的.tsconfig
  2. 确保您已在您的devDependencies 中安装"@types/lodash"

推理

简而言之: Typescript 的默认 import 机制与 ES6 的默认值不同。

例如,您可以通过以下方式从模块中导入函数:

  1. 整体导入:

    import _ from 'lodash';
    const results = _.uniq([1, 1, 2, 3]);
    
  2. 只导入一部分:

    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 as opposed to it being a flag?

【讨论】:

    【解决方案3】:

    您需要安装 lodash 库。

    "@types/lodash" 是使用 lodash 和 typescript 所需的 lodash 类型定义。

    要安装 lodash 运行:

    npm i --save lodash
    

    在终端中。

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 2017-12-05
      • 2021-11-24
      • 2019-01-24
      • 2020-11-28
      • 2021-03-25
      • 2017-05-22
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多