我正在一步一步地学习关于angular.io (角2)的教程。我在用打字本。在试图编译时,我会得到以下错误:
Cannot find external module 'angular2/angular2'
使用watch命令。
main.ts
import {Component, View, bootstrap} from 'angular2/angular2';
@Component({
selector: 'my-app'
})
@View({
template: '<h1>My first Angular 2 App</h1>'
})
class AppComponent {
}
bootstrap(AppComponent);index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://jspm.io/system@0.16.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.23/angular2.dev.js"></script>
</head>
<body>
<my-app></my-app>
<script>
System.import('main');
</script>
</body>
</html>Q为什么会出现这个错误?
发布于 2015-06-16 04:10:13
将这些文件添加到头部将为您提供最新的Angular2工作版本。
<script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script>
<script src="https://jspm.io/system@0.16.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.26/angular2.dev.js"></script>发布于 2015-09-15 08:52:33
缺少了用于TypeScript的Angular2定义:
- cd src //go to the directory where your ts-file is
- tsd install angular2 //load Angular2 TypeScript Definition然后再次编译(例如tsc -p src -w)
如果tsd失败,请先安装它:
npm install -g tsd@^0.6.0发布于 2015-12-14 16:49:50
根据相同的源5分钟快速启动,我将告诉您为什么接受的解决方案是坏的:
在柱塞上的活生生的例子中,我们在浏览器中动态地将文件(AKA编译)转到JavaScript。做个演示没问题。这不是我们对发展和生产的偏好。 我们建议在运行应用程序之前在构建阶段将transpiling (AKA编译)转换为JavaScript,原因包括:
更好的解决方案:
为您的angular2应用程序创建一个工作流,例如使用gulp创建ts编译任务。这里有一个很好的教程,我发现了用Gulp创建TypeScript工作流
您还可以使用vscode编辑器自动编译ts文件,阅读更多。
如果您感到太懒,无法创建自己的工作流,那么就有几个好的初学者可以使用angular2。每个工具都使用不同的工具,但它们都比使用运行时编译器更好。
https://stackoverflow.com/questions/30858164
复制相似问题