Angular2 网站在刷新除基础 URL 以外的任何页面时中断

Angular2 Website breaks on refreshing any page other than base URL

我开发了一个 angular2 网站,其中我实现了 angular2 路由并在生产环境中构建后部署了这个网站,现在当我从它的基础 URL 打开这个网站时,即 http://urlblocker.net/spotify it works fine. but when I navigate to any other page and refresh that page e.g http://urlblocker.net/spotify/about,它给出以下错误。

“未找到, 在此服务器上找不到请求的 URL /spotify/artist/0C8ZW7ezQVs4URX5aX7Kqx。 此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。"

如果我只是打开 URL 页,它也会产生同样的错误。 如何解决???

Angular2 SPA 需要 1 个应用程序入口点。如果不使用散列位置策略,任何不是入口点的 "page" 的刷新都将导致 404(该页面在服务器上不存在)。

解决这个问题的一种方法是在 app.module

中使用 HashLocationStrategy 作为提供者
providers: [
    {provide: LocationStrategy, useClass: HashLocationStrategy},
    ...
]

还要确保您的索引文件中有一个基本 href / 并且 app.module 中的导入语句需要包含

import {HashLocationStrategy, LocationStrategy} from "@angular/common";