angular2失败的lodash导入

时间:2016-02-01 12:35:25

标签: javascript ecmascript-6 angular

我开始使用简单的Angular 2应用程序。但是当我添加lodash的导入并尝试使用它时,我收到错误并且应用程序停止工作,并且无法弄清楚问题是什么。

我在控制台中收到这两个错误:

  

Uncaught SyntaxError:意外的令牌< __ exec @system.src.js:1374entry.execute @system.src.js:3300linkDynamicModule @system.src.js:2921link @system.src.js:2764execute @system.src。 js:3096doDynamicExecute @ system.src.js:715link @system.src.js:908doLink @system.src.js:569updateLinkSetOnLoad @system.src.js:617(匿名函数)@system.src.js:430run @ angular2- polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib $ es6 $ promise $$ internal $$ tryCatch @ angular2-polyfills.js:1511lib $ es6 $ promise $$ internal $$ invokeCallback @ angular2-polyfills.js:1523lib $ es6 $ promise $$ internal $$ publish @ angular2-polyfills.js:1494(匿名函数)@ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib $ es6 $ promise $ asap $$ flush @ angular2-polyfills.js:1305   angular2-polyfills.js:138

     

未捕获的SyntaxError:意外的令牌<   评估http://localhost:3000/lodash   加载http://localhost:3000/app/app.jsrun时出错@ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib $ es6 $ promise $$ internal $$ tryCatch @ angular2-polyfills.js:1511lib $ es6 $ promise $$ internal $$ invokeCallback @ angular2-polyfills.js:1523lib $ es6 $ promise $$ internal $$ publish @ angular2-polyfills.js:1494lib $ es6 $ promise $$ internal $$ publishRejection @ angular2-polyfills.js:1444(匿名函数)@ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib $ es6 $ promise $ asap $$ flush @ angular2-polyfills.js:1305

文件

的index.html

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

tsconfig.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.2",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "lodash": "^4.1.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "systemjs": "0.19.6",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.4",
    "typescript": "^1.7.5"
  }
}

的package.json

import {bootstrap}    from 'angular2/platform/browser'
import {Component} from 'angular2/core';
import * as _ from 'lodash';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App with lodash</h1>'
})
export class AppComponent {

    constructor(){
      console.log(_.last([1, 2, 3]));
  	  console.log('hello');
    }

}

bootstrap(AppComponent);

{{1}}

3 个答案:

答案 0 :(得分:14)

也许您可以在SystemJS级别尝试此配置:

System.config({
  (...)
  map: {
    lodash: 'node_modules/lodash/lodash.js'
  },
  meta: {
    lodash: { format: 'amd' }
  }
}

在TS文件中,您可以按照以下说明使用它:

import _ from 'lodash';
_.forEach([1,2,3], function(e) {
  console.log(e);
});

此问题可以帮助您:https://github.com/systemjs/systemjs/issues/951

亨利

答案 1 :(得分:2)

全力以赴:

安装所有直通终端:

npm install lodash --save
tsd install lodash --save

在index.html中添加路径

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

在.ts文件的顶部导入lodash

import * as _ from 'lodash'

答案 2 :(得分:0)

我解决了以下列方式导入lodash:

import * as _ from 'lodash';

在systemjs.config.js中我定义了这个:

map: { 'lodash' : 'node_modules/lodash/lodash.js' }