I'm trying to understand Angular's forRoot
and forChild
static methods.
I have two questions.
1) Are the following code snippets equivalent?
This:
// App Module
const routes = [
{ path: '', pathMatch: 'full', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent }
];
const routing: ModuleWithProviders = RouterModule.forRoot(routes);
with this:
// App Module:
const routes = [
{ path: '', pathMatch: 'full', component: HomeComponent },
];
const routing: ModuleWithProviders = RouterModule.forRoot(routes);
// Dashboard Module:
const routes = [
{ path: 'dashboard', component: DashboardComponent }
];
const routing: ModuleWithProviders = RouterModule.forChild(routes);
2) Do they work on the same object?
As I understand it, the forRoot
method is the one that instantiates the router services. It adds the first route. The forChild
than adds the second route to the same object.
Is that correct? Are forRoot
and forChild
working in the end on the same instance?