【问题标题】:In my angular webapp I get '404 error' when refresh在我的角度 webapp 中,刷新时出现“404 错误”
【发布时间】:2017-08-10 04:34:18
【问题描述】:

我有一个包含主页和详细信息页面的两页 Web 服务。当我双击主页数据时,我可以直接进入详细信息页面并填充详细信息。但是,当我按“F5”刷新页面时,总是得到“错误 404:未找到”。我试过使用'useHash:true'但仍然没有运气。有人可以请教吗?

我的 app-routing.module.ts 在下面;

const routes: Routes = [
  {path: '', redirectTo: '/dashboard', pathMatch: 'full'},
  {path: 'dashboard', component: DashboardComponent},
  {path: 'dashboard/detail', component: DetailComponent},

];

@NgModule({
  imports: [RouterModule.forRoot(routes, {  useHash: true, enableTracing: true })],
  exports: [RouterModule]
})
export class AppRoutingModule {
}

我的主页双击功能直接进入详情页:

  onRowDoubleClick(event) {
    this.routerService.navigate(["dashboard/detail"]);
  }

进入详细信息页面后,如果我从“F5”或 URL 刷新,我会收到以下错误;

ERROR

【问题讨论】:

    标签: angular http http-status-code-404 refresh angular2-routing


    【解决方案1】:

    如果您有一个虚拟目录并在 IIS 中托管,请尝试从基本 href 中删除“/”。

     <base href="">
    

    【讨论】:

    • 很高兴知道。如果有效,请将其标记为答案:)
    【解决方案2】:

    将此添加到 NgModule 中的 providers 数组中

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
    ],
      providers: [
        {provide: LocationStrategy, useClass: HashLocationStrategy}, //add this
      ],
      bootstrap: [AppComponent] 
    })
    

    【讨论】:

    • 嗨,谢谢你的回复,我也试过了,没有运气。我的 app.module.ts
    • @NgModule({ 声明:[ AppComponent, DashboardComponent, DetailComponent, ],导入:[ … ],模式:[],提供者:[{provide: LocationStrategy, useClass: PathLocationStrategy}],引导程序: [AppComponent] }) 导出类 AppModule { }
    • 对不起,我的错是HashLocationStrategy更新了答案
    • 不走运,仍然错误:HTTP ERROR 404 访问 / 时出现问题。原因:未找到
    • 你在 index .html 中的基本 href 是什么设置为 '/'
    猜你喜欢
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2022-10-31
    • 1970-01-01
    • 2015-04-21
    • 2021-10-12
    相关资源
    最近更新 更多