【问题标题】:Can't get Angular routing to work properly on refresh无法让 Angular 路由在刷新时正常工作
【发布时间】:2020-02-27 14:13:21
【问题描述】:

所以我有一个非常简单的路由情况设置,我在其中延迟加载模块:

const routes: Routes = [
  {
    path: '',
    loadChildren: './components/top-accounts/top-accounts.module#TopAccountsModule',
  },
  {
    path: 'tags',
    loadChildren: './components/tags/tags.module#TagsModule',
  },
  {
    path: ':accountId/details',
    loadChildren: './components/account-details/account-details.module#AccountDetailsModule',
  },
  { path: '**', redirectTo: '/' }
];

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

但是,当我刷新页面时,我的路由会变得一团糟,它会尝试默认使用 / 路由,即使 URL 显示类似 /tags/accountId1/details 的内容,然后它也会给我一个关于加载块的错误或页面直接空白且没有错误

如果我试图在页面加载时获取正确重定向的路由并显示正确的 url,并且不给我关于未能加载延迟加载的模块块的错误,我需要做一些简单的事情吗?

我目前在 Angular 7.2.0

【问题讨论】:

  • 你应该指定一个默认路由 { path: "", redirectTo: "account", pathMatch: "full" },你能在 tagsmodule 中提供你的路由吗?

标签: angular angular-routing


【解决方案1】:

请尝试一下:

RouterModule.forRoot(routes, { useHash: true })

而不是这个:

imports: [RouterModule.forRoot(routes)]

那个:

{ path: '**', redirectTo: '/', pathMatch: 'full' }

而不是这个:

{ path: '**', redirectTo: '/' }

并在此处查看更多信息:

Angular2 without hash in the urlNeed clarification RouterModule.forRoot([ ABOUT_ROUTE ], { useHash: true })

【讨论】:

  • 嘿,谢谢你的回答,我可以使用{ path: '**', redirectTo: '/', pathMatch: 'full' } 让它工作,而且我不必更改imports: [RouterModule.forRoot(routes)] :) 供将来参考,有没有办法做到这一点使用你知道的useHash
猜你喜欢
  • 2019-05-25
  • 1970-01-01
  • 2018-10-23
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2016-11-26
  • 2014-12-05
相关资源
最近更新 更多