将lodash导入app.ts文件 [英] Importing lodash into app.ts file

查看:370
本文介绍了将lodash导入app.ts文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lodash uniqBy函数在Angular 2应用程序中进行过滤.但是,我在import语句 import * as _ from'lodash'; 中遇到错误.我尝试从'lodash'导入从'lodash/lodash'导入_; 从'lodash/lodash'导入*作为_; ,但出现相同的错误,即找不到模块"lodash" .检查 angular2失败的lodash导入

I am trying lodash uniqBy function to filter in my Angular 2 application. But, I am getting error in import statement import * as _ from 'lodash';. I tried import _ from 'lodash';, import _ from 'lodash/lodash';, import * as _ from 'lodash/lodash'; also, but getting the same error i.e. Cannot find module 'lodash'. After checking angular2 failing lodash import, and Importing lodash into angular2 + typescript application, I mapped lodash to my system.config like so

<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

I have also added lodash to the dependencies in my package.json file like so

"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,但是即使那样,我仍然收到错误找不到模块'lodash'.

After npm install, I see lodash.js file here-->node_modules/lodash/lodash.js, but even then I am getting the error Cannot find module 'lodash'.

请分享您对这里可能出问题的想法.

Please share your thoughts on what might be going wrong here.

推荐答案

实际上,TypeScript应用程序包含两个部分:

In fact, there are two parts in TypeScript application:

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

通过引用SystemJS配置中的Lodash库,您只能处理执行部分.您的错误来自编译部分,因为您错过了为Lodash库安装键入内容.

By referencing the Lodash library in the SystemJS configuration, you tackle the execution part only. Your error comes from the compilation part since you missed to install typings for the Lodash library.

您可以尝试以下操作:

$ typings install lodash --ambient

这篇关于将lodash导入app.ts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆