【发布时间】:2016-05-09 21:54:46
【问题描述】:
我启动了简单的 Angular 2 应用程序,一切正常。但是当我添加 lodash 的导入并尝试使用它时,我得到了错误并且应用程序停止工作,并且无法弄清楚是什么问题。
我在控制台中收到以下 2 个错误:
Uncaught SyntaxError: Unexpected token <__exec :3096dodynamicexecute .js:138zoneboundfn angular2-polyfills.js:111lib angular2-polyfills.js:1511lib angular2-polyfills.js:1523lib angular2-polyfills.js:1494 angular2-polyfills.js:243run angular2-polyfills.js:138zoneboundfn angular2-polyfills.js:1305 angular2-polyfills.js:138>
Uncaught SyntaxError: Unexpected token 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
<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/app')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
package.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"
}
}
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);
【问题讨论】:
-
检查你是否安装了 lodash npm 包。
-
milan@milan-desktop:~/Desktop/newapp$ npm install -S lodash npm WARN package.json angular2-quickstart@1.0.0 没有描述 npm WARN package.json angular2-quickstart@1.0。 0 没有存储库字段。 npm WARN package.json angular2-quickstart@1.0.0 没有 README 数据 lodash@4.1.0 node_modules/lodash
-
我在 node_modules 和 package.json 中也看到了 lodash,那里似乎一切正常...
-
试试
'lodash/lodash' -
这里是安装的模块(来自终端的 ls): angular2 同时 es6-promise es6-shim lite-server lodash reflect-metadata rxjs systemjs twitter typescript zone.js
标签: javascript ecmascript-6 angular