AngularJS2 - 添加underscore.js - 找不到模块'下划线'

时间:2016-11-04 17:33:17

标签: angular underscore.js

希望有人能指出我在正确的方向吗?我正在尝试将underscore.js添加到我的AngularJS2应用程序中,但无法通过错误消息“无法找到模块'下划线'”。

我执行了:npm install underscore --save并且它添加了对我的package.json文件的引用:

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "docker-build": "docker build -t ng2-quickstart .",
    "docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
    "pree2e": "npm run webdriver:update",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "webdriver:update": "webdriver-manager update"
  },
  "keywords": [],
  "author": "",
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/material": "^2.0.0-alpha.9-3",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "ag-grid": "6.2.x",
    "ag-grid-ng2": "6.2.x",
    "angular-in-memory-web-api": "~0.1.13",
    "angular2-cool-storage": "^1.1.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "ng2-bootstrap": "^1.1.16",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "underscore": "^1.8.3",
    "zone.js": "^0.6.26"
  },
  "devDependencies": {
    "concurrently": "^3.1.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3",
    "canonical-path": "0.0.2",
    "http-server": "^0.9.0",
    "tslint": "^3.15.1",
    "lodash": "^4.16.2",
    "jasmine-core": "~2.5.2",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-htmlfile-reporter": "^0.3.4",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^4.0.9",
    "rimraf": "^2.5.4",
    "@types/core-js": "^0.9.34",
    "@types/node": "^6.0.45",
    "@types/jasmine": "^2.5.35",
    "@types/selenium-webdriver": "^2.53.32"
  },
  "repository": {}
}

这是我的systemjs.config.js文件,你可以看到我在那里添加了引用:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'ag-grid-ng2': 'node_modules/ag-grid-ng2',
      'ag-grid': 'node_modules/ag-grid',
      'ag-grid-enterprise': 'node_modules/ag-grid-enterprise',
      '@angular/material': 'npm:@angular/material/material.umd.js',
      'angular2-cool-storage': 'npm:angular2-cool-storage',
      'underscore': 'npm:underscore',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: { main: './main.js', defaultExtension: 'js' },
      rxjs: { defaultExtension: 'js' },
      'angular-in-memory-web-api': { main: './index.js', defaultExtension: 'js' },
      lib: { format: 'register', defaultExtension: 'js' },
      'ag-grid-ng2': { defaultExtension: "js" },
      'ag-grid': { defaultExtension: "js" },
      'angular2-cool-storage': { main: './cool-storage.js', defaultExtension: 'js' },
      'underscore': { main: './underscore.js', defaultExtension: 'js' },
      map: {
          'ag-grid-ng2': 'node_modules/ag-grid-ng2',
          'ag-grid': 'node_modules/ag-grid'
      }
    }
  });
})(this);

这是我的tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [ "./node_modules/@types/" ]
  },  
  "compileOnSave": true,
  "exclude": [ "node_modules/*", "**/*-aot.ts" ]
}

现在当我尝试添加:

import * as _ from 'underscore';

在我的组件中,我找不到模块下划线错误消息。

我已经将所有在线示例都跟踪到T并仍然出现此错误。

任何人都能看到我错过的东西吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

您很可能已经安装了下划线,您的应用程序将在运行时工作,但您缺少类型声明,因此typescript编译器不知道该模块并抱怨它。

要解决此问题,您需要通过typings或您用于此目的的任何工具安装下划线类型定义。

答案 1 :(得分:1)

也安装打字。

npm install --save underscore
npm install --save @types/underscore