将 Angular 7 部署到 github 页面

Rod*_*ndo 3 github-pages angular-routing angular angular7

我有一个简单的 Angular7 应用程序,它只有两条路线,主要是“文章”。如果你在本地测试它,它会起作用,但是当你放到 github 页面上时,它只会加载页面的 css。我按照文档中的角度文档进行了部署,并且还尝试了 gh-pages 工具。但它们都不起作用。如果我删除我的路线它就会起作用。

这是我在控制台上遇到的错误。

1 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'blogfolio'
Error: Cannot match any routes. URL Segment: 'blogfolio'
    at t.noMatchError (main.5443131190bc79920d7b.js:1)
    at e.selector (main.5443131190bc79920d7b.js:1)
  ............

rodnorm.github.io/:1 Failed to load resource: the server responded with a status of 404 () 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

应用程序.routing.ts

import { MainComponent } from './main/main.component';
import { ArticleComponent } from './article/article.component';    
import { Routes } from "@angular/router";

export const routes: Routes = [

    {
        path: '', component: MainComponent
    },
    {
        path: 'article', component: ArticleComponent
    }
];
Run Code Online (Sandbox Code Playgroud)

这是在articleList.component.html里面

<div class="post-preview" [routerLink]="['/article']">
Run Code Online (Sandbox Code Playgroud)

这是app.routing.module

import { routes } from './app.routing';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

如果你们想检查整个代码,请查看存储库这里是部署链接

Abh*_*ena 6

如果您的 Angular 应用程序中有路由器,那么它将在 GitHub 页面中不可用,并且您\xe2\x80\x99 将获得 404 页面。

\n

为了解决这个问题,你需要告诉服务器当找不到页面时需要做什么,因此复制index.html文件并将其重命名为404.html。这确保了所有未找到的路由将被重新路由到 Angular 路由器,并且它将正确处理这个问题。

\n

请参阅了解更多信息。谢谢!

\n