Angular 在 router-outlet 中渲染了 7 个重复的组件
Angular 7 duplicate component rendered in router-outlet
这种情况只是偶尔发生(通常在早上打开应用程序之后)。
我们正在使用 SwUpdate 更新应用程序,此行为似乎与更新时间一致。
结果 html 如下所示:
<app-root _nghost-vrt-c0="" ng-version="7.2.15">
<router-outlet _ngcontent-vrt-c0=""></router-outlet>
<app-layout _nghost-vrt-c4="">
<div _ngcontent-vrt-c4="" class="app-class">
<router-outlet _ngcontent-vrt-c4=""></router-outlet>
<app-component-1 _nghost-vrt-c7="">
</app-component-1>
<app-component-1 _nghost-vrt-c7="">
</app-component-1>
</div>
</app-layout>
</app-root>
问题是重复的 app-component-1。在正常情况下它只渲染一次。
我一直无法找到解决方案,甚至找不到描述此问题的问题。
感谢任何帮助。
相关路由规则:
const routes: Routes = [
{
path: '',
component: Layout,
canActivate: [AuthGuard],
children: [
{ path: 'home', loadChildren: './home/home.module#HomeModule'},
{ path: 'otherRoute', loadChildren: './otherRoute/otherRoute.module#OtherRouteModule'},
{ path: '', redirectTo: 'home', pathMatch: 'full' },
]
}
{ path: 'under-construction', component: UnderConstructionComponent, data: { title: 'Under Construction' } },
{ path: '**', redirectTo: 'under-construction' },
]
这里是布局模板:
<div class="tm-app">
<div >
<app-menu></app-menu>
<div>
<div>
<app-top-bar></app-top-bar>
</div>
<router-outlet></router-outlet>
</div>
</div>
我尽可能多地清理了布局和输出以保证代码库的安全。
我正在编辑这个问题来解决它。因为我还没有找到解决办法。这样可以吗?
在我的特殊情况下,问题与实施 CanActivate
的路由守卫有关。
当守卫returns false
时,路由无法激活(但已经创建)。如果守卫同时也重定向到另一条路由,则会创建一个重复的路由器出口组件。
解决方案:不要从路由守卫重定向 CanActivate
。只有 return 真或假。
替代解决方案:从 CanActivate
守卫重定向并始终 return true(在守卫中重定向时)。
可以从警卫处 return 找到一条替代路线,这比总是 return 正确的解决方案更好。
您可以尝试删除 app.component.html
中的 component
,router
出口除外。
这种情况只是偶尔发生(通常在早上打开应用程序之后)。
我们正在使用 SwUpdate 更新应用程序,此行为似乎与更新时间一致。 结果 html 如下所示:
<app-root _nghost-vrt-c0="" ng-version="7.2.15">
<router-outlet _ngcontent-vrt-c0=""></router-outlet>
<app-layout _nghost-vrt-c4="">
<div _ngcontent-vrt-c4="" class="app-class">
<router-outlet _ngcontent-vrt-c4=""></router-outlet>
<app-component-1 _nghost-vrt-c7="">
</app-component-1>
<app-component-1 _nghost-vrt-c7="">
</app-component-1>
</div>
</app-layout>
</app-root>
问题是重复的 app-component-1。在正常情况下它只渲染一次。 我一直无法找到解决方案,甚至找不到描述此问题的问题。 感谢任何帮助。
相关路由规则:
const routes: Routes = [
{
path: '',
component: Layout,
canActivate: [AuthGuard],
children: [
{ path: 'home', loadChildren: './home/home.module#HomeModule'},
{ path: 'otherRoute', loadChildren: './otherRoute/otherRoute.module#OtherRouteModule'},
{ path: '', redirectTo: 'home', pathMatch: 'full' },
]
}
{ path: 'under-construction', component: UnderConstructionComponent, data: { title: 'Under Construction' } },
{ path: '**', redirectTo: 'under-construction' },
]
这里是布局模板:
<div class="tm-app">
<div >
<app-menu></app-menu>
<div>
<div>
<app-top-bar></app-top-bar>
</div>
<router-outlet></router-outlet>
</div>
</div>
我尽可能多地清理了布局和输出以保证代码库的安全。 我正在编辑这个问题来解决它。因为我还没有找到解决办法。这样可以吗?
在我的特殊情况下,问题与实施 CanActivate
的路由守卫有关。
当守卫returns false
时,路由无法激活(但已经创建)。如果守卫同时也重定向到另一条路由,则会创建一个重复的路由器出口组件。
解决方案:不要从路由守卫重定向 CanActivate
。只有 return 真或假。
替代解决方案:从 CanActivate
守卫重定向并始终 return true(在守卫中重定向时)。
可以从警卫处 return 找到一条替代路线,这比总是 return 正确的解决方案更好。
您可以尝试删除 app.component.html
中的 component
,router
出口除外。