刷新相同 URL 给出 404 not found in angular

Refreshing same URL gives 404 not found in angular

我已经在服务器上托管了我的 angular 应用程序。当我单击服务器 url 时,它会将我重定向到登录页面。但是,当我刷新相同的 url 后缀登录时,它给我 404 未找到错误。 原来给的url是没有登录后缀的。 请帮忙。

在app.routing.module.ts中,我配置了这样的路由器路径:

Angular 有时会发生这种情况。我看到发生这种情况的原因有几个。一个是因为 URL 使用简单的 js 发生更改,并且由于给定的 link 上没有内容,所以阻止网络爬虫。无论如何,如果你想要快速修复,你可以使用哈希方法。这种方法将使您的 link 看起来像这样: http://link/#/pagehttp://link/page 相比(注意 #

选项 1

在您的 app.module.ts 中,为 HashLocationStrategy, LocationStrategy 添加导入:

import { HashLocationStrategy, LocationStrategy } from '@angular/common'; ,然后在你的 NgModule Provider 中:

{provide: LocationStrategy, useClass: HashLocationStrategy}

这可能类似于以下内容:

App.Module.Ts

import { NgModule }       from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';

@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule],
    providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
    bootstrap: [AppComponent],
})
export class AppModule {}

选项 2

另一种方法是使用 RouterModule.forRoot...useHash: true 参数。

在您的路由模块或应用程序模块中

@NgModule({
  imports: [
    ...
    ...
    RouterModule.forRoot(routes, { useHash: true })
  ],

选项 3

如果这些方法不起作用,或者它们没有给你想要的结果(你可能不想要 has 方法),那么我会建议研究 AngularUniversal,或者研究服务器端方法将您的服务器配置为为定义的每个路由提供 index.html 文件。这是一个更长更复杂的方法,但希望以上选项就足够了。