尝试学习 Angular,所以我从他们网站 ( https://angular.io/guide/quickstart ) 中的 TypeScript 快速入门教程开始
我正在使用 ubuntu 14.04。
我已经完成了每个步骤,最后我没有收到任何错误,但组件没有加载。相反,我唯一看到的是“正在加载...”,而不是主要组件“我的应用程序”。
index.html:
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
app/app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
我认为显示其余配置文件没有意义,因为它们与教程中的完全一样。唯一的变化是我将 tsconfig.json 更改为
"目标": "es6",
因为它在其他情况下不起作用。
可能是什么问题?为什么它没有加载我的主要组件?
编辑:这些是我的浏览器控制台中的错误:
2 http://localhost:3000/node_modules/systemjs/dist/system.src.js Failed to load resource: the server responded with a status of 404 (Not Found)
systemjs.config.js:46 Uncaught TypeError: System.config is not a function
http://localhost:3000/app/ Failed to load resource: the server responded with a status of 404 (Not Found)
app:18 Not Found: http://localhost:3000/app
Error loading http://localhost:3000/app(anonymous function) @ app:18
http://localhost:3000/app/ Failed to load resource: the server responded with a status of 404 (Not Found)
最佳答案
我遇到了类似的问题,终于找到了问题所在。
在我的例子中,当我在浏览器控制台中运行 npm start
时,错误是 XHR error (404 Not Found) loading http://localhost:3000/app/main.js
。我意识到我缺少 main.js
,但我有 /app/main.ts
。
Typescript 文件 (.ts) 未编译为 JS(.js) 文件。然后发现tsconfig.json文件末尾多了一个字符“\”。它在资源管理器或 Atom 中不可见,直到在终端中键入 tsconfig
并按下 Tab 键以自动完成。它表明文件名末尾有一个附加字符。然后使用此命令 $ mv tsconfig.json\tsconfig.json
,更改名称。然后使用 npm start,它运行良好。
所以请在控制台中检查所有文件名。希望你的也能正常工作。
关于Angular 2 Quickstart - 我的应用程序组件未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752064/