Sar*_*wan 0 http-status-code-404 angular angular8
我在服务器上托管了我的 angular 应用程序。当我单击服务器 url 时,它会将我重定向到登录页面。但是,当我刷新以 login 为后缀的相同 url 时,它给了我 404 not found 错误。给出的原始网址没有登录后缀。请帮忙。
在 app.routing.module.ts 中,我配置了这样的路由器路径:
Angular 有时会发生这种情况。我所看到的发生这种情况的原因有多种。一个是因为使用简单的 js 发生 URL 更改,并且由于给定链接上没有内容,因此还可以阻止网络爬虫。无论如何,如果您想要快速修复,您可以使用哈希方法。这种方法将使您的链接看起来像这样:
http://link/#/page与http://link/page(注意#)
在您的 中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 {}
Run Code Online (Sandbox Code Playgroud)
另一种方法是RouterModule.forRoot...与useHash: truearg一起使用。
在您的路由模块或应用程序模块中:
@NgModule({
imports: [
...
...
RouterModule.forRoot(routes, { useHash: true })
],
Run Code Online (Sandbox Code Playgroud)
如果这些不起作用,或者它们没有给您想要的结果(您可能不想要 has 方法),那么我建议您查看 AngularUniversal,或查看服务器端方法来配置您的服务器以提供 index.html定义的每个路由的文件。这是一种更长更复杂的方法,但希望上述选项就足够了。
| 归档时间: |
|
| 查看次数: |
2882 次 |
| 最近记录: |