本文介绍了Angular2网站在刷新除基本URL之外的任何页面时均中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个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},
    ...
]

还请确保/的索引文件中具有基本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之外的任何页面时均中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:53