我在我的应用程序的根路由模块中使用了以下路由:
const routes: Routes = [
{
path: '',
children: [
{
path: 'home',
component: HomeDetailsComponent,
},
{
path: 'home',
component: HomeDetailsComponent,
children: [
{
path: 'room/:id',
component: RoomDetailsComponent,
},
]
},
{
path: 'sectorNumber',
component: SectorNumberComponent
},
{
path: '**',
redirectTo: 'home',
},
]
}
];现在,在HomeDetailsComponent内部,我使用OnInit生命周期钩子来调用http get方法,并在此基础上进行一些事件处理。然而,我注意到在这种路由方式下,我的HomeDetailsComponent被初始化了两次。一次是我导航到'http://localhost:3000/#/curriculum/',第二次是我导航到'http://localhost:3000/#/curriculum/chapter/1'路由。谁能告诉我为什么会这样?
发布于 2017-02-01 23:16:27
因为组件不会被重用于不同的路由。您导航离开的路径中的组件将被销毁,而您导航到的组件将被创建。只有当起始路由和终止路由相同但参数值(:id)改变时,这才有所不同。
https://stackoverflow.com/questions/41983089
复制相似问题