【发布时间】:2016-04-20 09:48:23
【问题描述】:
我一直在尝试编写 Angular2 的快速入门指南。我按照快速指南中的说明做了示例。但是,当我运行它时,它显示以下消息“无法获取”。有谁知道为什么会这样?
boot.js 文件
// JavaScript source code
(function (app) {
document.addEventListener('DOMContentLoaded', function () {
ng.platform.browser.bootstrap(app.AppComponent);
});
})(window.app || (window.app = {}));
app.component.js 文件
(function (app) {
app.AppComponent = ng.core.Component({
Selector: 'my-app',
template: '<h1>My First Angular 2 App </h1>'
})
.class({
constructor: function () { }
});
})(window.app || window.app == {});
索引文件
<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/Rx.umd.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-all.umd.dev.js"></script>
<!-- 2. Load our 'modules' -->
<script src='app/app.component.js'></script>
<script src='app/boot.js'></script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
最后是 package.json 文件
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "npm run lite",
"lite": "lite-server"
},
"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": {
"lite-server": "^1.3.1"
}
}
我运行了“npm start”行,它打开了浏览器并显示“无法获取”
【问题讨论】:
-
当服务器无法获取文件时无法获取 (404)。我遇到了同样的问题。 Http Server 在触发它的目录中查找 index.html。确保将 index.html 放在运行 http 服务器的文件夹根级别。另外,根据您的 html 文件,确保 app.component.js 和 boot.js 在 app 文件夹下。希望对您有所帮助。
-
我在使用 ng serve 时遇到了同样的问题。但在我的情况下,保存 Angular 应用程序的文件夹存在一些权限问题。
标签: javascript node.js angular