问题描述
我开发了一个 angular2 网站,我在其中实现了 angular2 路由并在生产环境中构建了它之后部署了这个网站,现在当我从它的基本 URL 打开这个网站时,即 http://urlblocker.net/spotify 它工作正常.但是当我导航到任何其他页面并刷新该页面时,例如 http://urlblocker.net/spotify/about,它给出以下错误.
I developed an angular2 website in which I implemented angular2 routes and deployed this website after building it in production environment, now when I open this website from its base URL i.e 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, it gives following error.
未找到,在此服务器上找不到请求的 URL/spotify/artist/0C8ZW7ezQVs4URX5aX7Kqx.此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误."
"Not Found,The requested URL /spotify/artist/0C8ZW7ezQVs4URX5aX7Kqx was not found on this server.Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."
如果我只是打开关于页面 URL,它也会产生同样的错误.怎么解决???
If I simply open about page URL it also prduces same error.How to resolve it???
推荐答案
Angular2 SPA 需要应用程序的 1 个单一入口点.如果不使用哈希定位策略,任何不是入口点的页面"的刷新都会导致 404(该页面在服务器上不存在).
An Angular2 SPA expects 1 single entry point for the app. Without using hash location strategy any refresh of a "page" that isn't the entry point will result in a 404 (the page doesn't exist on the server).
解决这个问题的一种方法是使用 HashLocationStrategy 作为 app.module 中的提供者
One way to solve this is by using the HashLocationStrategy as a provider within app.module
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy},
...
]
还要确保在/的索引文件中有一个 base href 并且 app.module 中的 import 语句需要包含
Also make sure you have a base href in your index file of / and your import statement within app.module needs to include
import {HashLocationStrategy, LocationStrategy} from "@angular/common";
这篇关于Angular2 网站在刷新除基本 URL 之外的任何页面时中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!