Angular2快速入门 - “Cannot Get”消息

31

我一直在尝试完成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)时,会出现“Cannot GET”的错误。我也遇到了同样的问题。Http服务器在触发它的目录中查找index.html。请确保您将index.html放置在运行http服务器的文件夹根级别上。此外,请确保app.component.js和boot.js位于与您的html文件相应的app文件夹下。希望这可以帮助您。 - Nitin
在使用ng serve时,我遇到了同样的问题。但是在我的情况下,这是由于存放Angular应用程序的文件夹内部的某些权限问题所致。 - Ashutosh Singh
10个回答

55

当 index.html 不在正确的文件夹中时,我遇到了这个错误。

如果是这种情况,将其放置在主 Angular 项目的根文件夹中应该可以解决该错误。


6
这意味着在 angular2-quickstart 内部。你可能会在 app 文件夹中找到它。 - Marques
3
非常感谢您的帮助。如果您正在参考 https://angular.io/docs/ts/latest/quickstart.html 中的教程,那么只移动 'index' 和 'Styles.CSS' 页面就可以解决这个问题。请注意,在开始时添加教程会让人们感到困惑并产生不满。 - AltF4_
4
有没有办法覆盖这种行为?比如说将路径改为/src/app和/src/index.html而不是/app和/index.html? - Cristian E.

16

背景

我有同样的问题:Cannot GET / pagename 在每个页面上。我在互联网上搜索,但没有找到像这样的解决方案。

我的项目位于一个带有ASCII空格的文件夹中,因为存储库名称也包含了空格。

答案

您的项目所在的文件夹不应包含任何空格或ASCII字符“%20”。

enter image description here

更改后,我的项目不再出现 Cannot GET / pagename 错误。


7
这个答案应该得到更多的投票,我已经花费了超过10个小时在这个问题上。 - Carlo Luther
1
@Luther,是的,我也花了很长时间,我在互联网上没有看到这个解决方案。 - Brampage
@Brampage 感谢你的答案。在找到你的答案之前,我花了几个小时调试我的代码。 - walexy

3

当你的Angular项目无法编译时,会出现“Cannot GET /”错误。因此,请首先确保您的Angular代码没有任何错误,并且可以正常编译。只需使用以下命令:

ng serve

它应该列出你的Angular代码存在的所有问题。

3

我找出了问题所在,我的html文件中有一个额外的空格导致了错误。我删除了这个额外的空格,然后它就按照预期工作了。


2
请问您能提供关于“额外空间”问题的更多信息吗? - Aleksander Pohl
遇到了同样的问题。多余的空格在index.html文件末尾。 - otc
ng serve 运行时出现了一些额外空间卡顿的情况,是否有永久性解决方案? - otc
这是我的笔误,与Angular无关。 - Josiane Ferice

2

我遇到了同样的问题,因为我忘记添加教程中“最终结构”部分中所见的bs-config.json文件。它应该包含以下内容:

{
  "server": {
    "baseDir": "src",
    "routes": {
      "/node_modules": "node_modules"
    }
  }
}

1
如果您正在使用路由,请确保您有一个正确导入的组件指向“/”(/路由)。
例如: { path: "", component: MyHomeComponent } 以下是Angular 5安装的app.module.ts的完整示例(与Angular +2相同...)
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { HttpModule } from "@angular/http";
import { Routes, RouterModule } from "@angular/router";

import { AppComponent } from "./app.component";
import { CinemaService } from "./services/cinema.service";
import { MyHomeComponent } from "./components/my-home/my-home.component";
import { MyMovieComponent } from "./components/my-movie/my-movie.component";

const routes: Routes = [
  { path: "", component: MyHomeComponent },
  { path: "movie/:id", component: MyMovieComponent },

];

@NgModule({
  declarations: [
    AppComponent,
    MyHomeComponent,
    MyMovieComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(routes)
  ],
  providers: [CinemaService],
  bootstrap: [AppComponent]
})
export class AppModule { }

0
你需要在“src”文件夹所在的位置运行“ng serve”命令。

0

我在使用Angular.io的"minimal NgModule"示例时遇到了同样的问题,他们给我们提供的文件名中有额外的"0"。 我通过将文件名"index.0.html"转换为"index.html"来解决这个问题。 最终它成功运行了 :)


-1

除了Melaz所说的,我也遇到了同样的问题,只是一个HTML文件(没有React、Angular),我通过将文件名更改为index.html解决了这个问题。


-1

我因为在angular-cli.json文件中做了一些更改,然后运行我的应用程序时遇到了这个错误。

 " Cannot GET / ".

然后我移除了在 angular-cli.json 文件中做的修改并运行了应用,一切都正常。


“一些变化”是什么? - Brampage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接