angular2 lodash 导入失败
Posted
技术标签:
【中文标题】angular2 lodash 导入失败【英文标题】:angular2 failing lodash import 【发布时间】: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
【参考方案1】:
也许您可以在 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。
蒂埃里
【讨论】:
我只需要在 system.config 中添加“map”部分,它现在就可以工作了!非常感谢!【参考方案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'
【讨论】:
tsd 已被贬低,而必须使用打字【参考方案3】:我通过以下方式解决了导入 lodash:
import * as _ from 'lodash';
在 systemjs.config.js 中我定义了这个:
map: 'lodash' : 'node_modules/lodash/lodash.js'
【讨论】:
以上是关于angular2 lodash 导入失败的主要内容,如果未能解决你的问题,请参考以下文章
Lodash - '_'指的是UMD全局,而lodash.js不是模块错误