无法使 Angular 路由在刷新时正常工作
Can't get Angular routing to work properly on refresh
所以我有一个非常简单的路由情况设置,其中我懒加载模块:
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
请尝试:
RouterModule.forRoot(routes, { useHash: true })
而不是这个:
imports: [RouterModule.forRoot(routes)]
那个:
{ path: '**', redirectTo: '/', pathMatch: 'full' }
而不是这个:
{ path: '**', redirectTo: '/' }
并在此处查看更多信息:
和
所以我有一个非常简单的路由情况设置,其中我懒加载模块:
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
请尝试:
RouterModule.forRoot(routes, { useHash: true })
而不是这个:
imports: [RouterModule.forRoot(routes)]
那个:
{ path: '**', redirectTo: '/', pathMatch: 'full' }
而不是这个:
{ path: '**', redirectTo: '/' }
并在此处查看更多信息: