【问题标题】:Importing lodash into app.ts file将 lodash 导入 app.ts 文件
【发布时间】:2016-11-17 22:23:20
【问题描述】:

我正在尝试使用 lodash uniqBy 函数在我的 Angular 2 应用程序中进行过滤。但是,我在导入语句 import * as _ from 'lodash'; 中遇到错误。我试过 import _ from 'lodash';, import _ from 'lodash/lodash';, import * as _ from 'lodash/lodash'; strong> 也是,但得到相同的错误,即 找不到模块 'lodash'。检查angular2 failing lodash importImporting lodash into angular2 + typescript application 后,我将lodash 映射到我的system.config,如下所示

<script>
        System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        },
        map: {
          lodash: 'node_modules/lodash/lodash.js'
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

我还在 package.json 文件中的依赖项中添加了 lodash,就像这样

"dependencies": {
        "angular2": "2.0.0-beta.15",
        "systemjs": "0.19.26",
        "es6-shim": "^0.35.0",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.2",
        "zone.js": "0.6.10",
        "bootstrap": "^3.3.6",
        "lodash":"^4.13.1"
    }
安装 npm 后,我在此处看到 lodash.js 文件-->node_modules/lodash/lodash.js,但即便如此,我仍然收到错误 Cannot find module 'lodash'

请分享您对这里可能出现的问题的看法。

【问题讨论】:

    标签: angular lodash


    【解决方案1】:

    其实TypeScript应用有两部分:

    • 带有类型检查的编译部分
    • 代码实际执行时的执行部分

    通过在 SystemJS 配置中引用 Lodash 库,您只需处理执行部分。您的错误来自编译部分,因为您错过了为 Lodash 库安装类型。

    您可以尝试以下方法:

    $ typings install lodash --ambient
    

    【讨论】:

    • 环境标志似乎已被弃用,因此必须使用全局。它仍然没有工作。收到超时消息并以错误结束。打字错误!消息无法连接到“api.typings.org/entries/npm/l odash/versions/latest”类型错误!由连接 ETIMEDOUT 104.24.113.177:443 打字错误引起!节点 -v v4.4.5 分型错误!打字 -v 1.3.1 打字错误!代码 EUNAVAILABLE
    • 显然我不必完成所有这些步骤。只需在 index.html
    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2016-05-09
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多