无法在angular2项目中导入lodash

时间:2017-07-29 00:13:05

标签: angular ionic2

我有一个用ionic2写的应用程序,而且我使用的是lodash。在命令行和npm install lodash上使用简单的import * as _ from 'lodash',我可以通过lodash访问_,无任何问题

我已经在angular2 cli开了一个新的应用程序,这就是离子的基础,并且我试图做同样的事情。我已经在命令行和npm install lodash上运行import * as _ from 'lodash'但是一旦我尝试编译应用程序,我得到ERROR in {DIRECTORY_PATH}/src/app/otherlists/otherlists.component.ts (2,20): Cannot find module 'lodash'.,我似乎无法使其正常运行。我甚至复制了lodash中的node_modules文件夹,但没有区别......

我错过了什么?

  

EDIT,

为了进一步证明这一点,我从头开始创建了一个全新的应用程序。 ng new parser 这创造了一个新的角度项目。下一个命令是 npm install --save lodash并使用import * as _ from 'lodash';将库导入我的项目 一旦我尝试使用ng serve运行它,我收到以下错误:

ERROR in C:/dev/ionic/parser/src/app/mainpage/mainpage.component.ts (2,20): Cannot find module 'lodash'.

如果我运行npm install --save-dev @types/lodash,我会收到一长串以

开头的错误
ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8604,32): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8607,14): ']' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8607,15): ';' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,26): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,42): '(' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,61): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,66): ')' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8612,15): ';' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8612,25): Declaration or statement expected.

对于子孙后代,请点击package.json

{
  "name": "parser",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "core-js": "^2.4.1",
    "lodash": "^4.17.4",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/lodash": "^4.14.71",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.28.3",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.3.0",
    "typescript": "~2.0.3"
  }
}

1 个答案:

答案 0 :(得分:2)

您需要安装包两种类型:

npm install --save lodashnpm install --save-dev @types/lodash

然后,您可以使用import * as _ from 'lodash'

以正常方式访问这些类型

This github repository显示了使用Angular 4和Angular CLI 1.3的示例。