Angular:无法获得/

Kly*_*ner 27 javascript node.js typescript angular

我正在尝试打开,构建和运行其他人的Angular 4项目,但是当我按照自己的方式运行时,我无法查看项目.我不知道出了什么问题或我现在应该做些什么.我已经拥有了使用NPM和NodeJS的一切

我采取的步骤是:

  • 打开项目
  • npm安装
  • 服务

该项目以正确的方式编制.(我有一个自己的Angular应用程序,我知道这是怎么样的)控制台显示:

'**NG Live Development Server正在侦听localhost:4200,在http:// localhost:4200**' 上打开浏览器.

然后,当我打开一个Web浏览器时,我导航到localhost:4200并显示了一个带有以下文本的网页:

'不能获取 /'

在控制台上有以下文字:

'GET http:// localhost:4200/404(Not Found)'

该项目应该可以正常工作,但我无法导航到网页上的工作URL.路由设置是另一种方式,因为我习惯这样做.在app.module.ts中实现了以下内容:

app.module.ts

const appRoutes: Routes = [
{ path: '',   redirectTo: 'tree', pathMatch: 'full' },
{ path: 'admin', component: AdminPanelComponent, canActivate: [AuthGuard],
children: [{path:'', component: PanelComponent},{path: 'add', component: 
AddTreeComponent}, {path:'manage-trees', component:ManageTreesComponent}, 
{path:'manage-users', component: ManageUsersComponent}, {path:'view-trees', 
component: ViewTreeComponent}]},
{path:'tree', component: TreeComponent},
{path:'error', component: ErrorComponent},
{path:'unauthorized', component: UnauthorizedComponent},
{path:'login', component: LoginComponent},
{path:'entire-tree', component: EntireTreeComponent},
{ path: '**', component: PageNotFoundComponent },
];
Run Code Online (Sandbox Code Playgroud)

还打开了一个网页; localhost:4200 /树不起作用.当我让角停止提供网页,该网页显示:"该网站无法达到"所以我认为这是在本地主机上运行的东西:4200 ...此外,这个人的另外一个项目的工作方式.

在此输入图像描述

有人知道发生了什么吗?

编辑

app.module.ts

RouterModule.forRoot(appRoutes, { useHash: true })
Run Code Online (Sandbox Code Playgroud)

的package.json

{
"name": "xxx",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.0.0",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"angular-oauth2-oidc": "^1.0.20",
"angular-polyfills": "^1.0.1",
"angular2-jwt": "^0.2.3",
"angular2-spinner": "^1.0.10",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"ngx-bootstrap": "^1.8.0",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.2.4",
"@angular/compiler-cli": "^4.0.0",
"@angular/language-service": "^4.0.0",
"@types/jasmine": "2.5.45",
"@types/node": "~6.0.60",
"codelyzer": "~3.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^1.2.1",
"protractor": "~5.1.2",
"ts-node": "~3.0.4",
"tslint": "~5.3.2",
"typescript": "~2.3.3"
 }
}
Run Code Online (Sandbox Code Playgroud)

我还在选项卡名称旁边看到一个带有标签的图标:"错误".

观察:

新观察:

我跑了之后我npm install -g angular-cli无法跑ng serve.(你必须在angular-cli项目中才能在重新安装angular-cli之后使用build命令)

然后我跑了npm install -g @angular/cli@latest,我能够ng serve再次使用.

观察2:

用'ng build ...'构建应用程序后,'dist'文件夹中没有index.html ...当我在线设置网站时,只有一个文件夹结构而不是一个不错的网站.我认为这是因为没有index.html.

Din*_*hNS 31

解决该错误的方法是查找并修复控制台报告的错误。

ng build在您的命令行/终端中运行,它应该显示一个有用的错误,例如此处的红色示例:类型'object'上不存在属性'name'

控制台示例

  • `ngserve` 也可以运行构建/编译。两个命令都会出现相同的消息。 (4认同)

yez*_*ior 19

对我来说这也是路径问题,但我在根文件夹中有百分号.

用空格替换%20后,它开始工作:)

  • 我正在用代码编写主文件夹的名称,例如我从 Azure Devops 克隆了项目“My Project”,它在我的磁盘上创建了文件夹“My%20Project” (4认同)
  • 这太完美了,拯救了我的一天。基本上我从 azure devops 克隆了存储库,它的存储库名称中有空格。非常感谢。 (3认同)

Kly*_*ner 10

问题是我在文件夹中运行了命令/project/src/app.导航回项目文件夹以便从那里/project运行ng serve并解决了我的问题.


J.K*_*rk. 9

我遇到了由构建错误引起的相同错误.我在我的应用程序目录中运行了build,这帮助我纠正了我的错误

  • 能够诊断错误非常有帮助。谢谢 (2认同)

Sal*_*rcı 5

index.html检查是否设置了in base

<head>
  <base href="/">
  ...
</head>
Run Code Online (Sandbox Code Playgroud)


小智 5

我在 Angular 9 上遇到了同样的问题。

就我而言,我将 angular.json 文件从

"aot": true
Run Code Online (Sandbox Code Playgroud)

"aot": false
Run Code Online (Sandbox Code Playgroud)

这个对我有用。