Angular 路由模块的Ngonit生命周期钩子行为?
我正在应用程序的根路由模块中使用以下路由:Angular 路由模块的Ngonit生命周期钩子行为?,angular,angular2-routing,Angular,Angular2 Routing,我正在应用程序的根路由模块中使用以下路由: const routes: Routes = [ { path: '', children: [ { path: 'home', component: HomeDetailsComponent, }, { path: 'home', component: HomeDetailsComponent, children:
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方法,并在此基础上执行一些事件处理。然而,我注意到,通过这种路由,我的HomeDetails组件被初始化了两次。一次当我导航到'http://localhost:3000//curriculum/'当我导航到'http://localhost:3000//curriculum/chapter/1“路线。有人能告诉我为什么会发生这种情况吗?因为该组件没有被用于不同的路由。将销毁您导航离开的路线中的组件,并创建您导航到的组件。这仅在from route和to route相同但参数值:id更改时不同。如果from route和to route相同但参数值:id更改,则不会重新初始化,这是您的意思吗?确切地说。因为我也尝试过同样的方法,在这种情况下,它也在重新初始化。我将从“xxx/home”转到“xxx/home/2”,可以看到它正在重新初始化。但这是两个不同的路由,一个没有参数,另一个有参数。您必须配置两条路由,才能做到这一点。