我正在做Angular2 5分钟的快速启动.
现在大约一半的教程,我正确设置了以下文件:
index.html的,
app.component.ts
应用程序/ boot.ts
的package.json
tconfig.json
跑了npm start
,我收到这个错误:
Uncaught ReferenceError: require is not defined(anonymous function) @ boot.js:1
angular2-polyfills.js:143
Uncaught TypeError: Cannot read property 'split' of undefinedreadMemberExpression @ system.src.js:1456(anonymous function) @ system.src.js:3224(anonymous function) @ system.src.js:3749complete @ system.src.js:2487run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111
我找到了关于使用es6垫片和我包含的链接.
但是我仍然得到 Uncaught ReferenceError: require is not defined
错误.
import {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: 'My First Angular 2 App
' }) export class AppComponent { }
import {bootstrap} from 'angular2/platform/browser' import {AppComponent} from './app.component' bootstrap(AppComponent);
Angular 2 QuickStart Loading...
{ "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.0", "systemjs": "0.19.6", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.0", "zone.js": "0.5.10" }, "devDependencies": { "concurrently": "^1.0.0", "lite-server": "^1.3.1", "typescript": "^1.7.3" } }
{ "compilerOptions": { "target": "ES5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules" ] }
npm start
我发现我带来错误的原因是我的tsconfig.json文件中将模块设置为"commonjs" ,将其更改为系统修复它,现在看起来如下所示
{
"compilerOptions": {
"target": "es5",
"module": "system",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
编辑 这个答案是基于Angular 2 beta 17.根据这个答案的时间和角度2经历的快速变化,这可能不再适用了.
我在angular2.beta15版本中遇到了相同的错误.
我通过删除解决了它
format: 'register',
超出index.html
好吧终于让我的'基本'应用程序工作了.
首先我的问题是npm start
没有编译我的打字稿.ts
文件.
从这篇文章中,我在这里找到了答案找不到外部模块'angular2/angular2' - Angular2 w/Typescript
我需要运行npm install -g tsd@latest
来更新我的TypeScript定义.在那之后,我需要更新Angular2 的TypeScript定义(TSD)tsd install angular2
.
完成此操作后,我仍然从boot.js文件中收到错误.
而不是这个 import {AppComponent} from './app.component'
我需要这样写 import {AppComponent} from '../app.component.js'
现在它有效! https://github.com/leongaban/angular2quickstart
一个恼人的问题是npm start
仍然没有自动编译打字稿文件,所以我仍然需要手动编译每个.ts文件tsc app.component.ts --module system