I have a BaseComponent
. on each child navigation BaseComponent
constructor
and onInit
are called.
export class BaseCoponent implements OnInit {
contstructor(...){
console.log("constructor BaseComponent");
}
ngOnInint(): void {
console.log("ngOnInit BaseComponent");
}
}
routing for Base
const routes = Routes = [
{
path: '',
component: BaseComponent
children: [
{
path: '',
component: Child1Component
},
{
path: 'child2',
component: Child2Component
}
]
}
]
export const routing = RouterModule.forChild(routes);
Child1Component
export class Child1Component {
onAction(){
this.router.navigate(['/base/child2']);
}
}
On Navigating both the constructor
log and the onInit
of BaseComponent
are called. I'm attaching the BaseModule
and app routing
at the end of the question
few questions
- Is it a normal behavior? for what i understand
BaseComponent
should be aSingleton
and live for as long as not navigating away from the base (usingforRoot
). - If this is the "correct" behavior can i control it in some manner such as
BaseComponent
will be a Singleton? - Are also the children
singletons
? navigating away fromChild1Component
and then back toChild1Component
should create a new instance or return the same instance? Can i control the behavior in some matter?
app.route
const appRoutes: Routes = [
{
path: 'base'
loadChildren: 'app/base-module#BaseModule,
canActivate: [AuthGuard]
}...
}
export const appRoutingProviders: any[] = [
authProviders
];
export const routing = RouterModule.forRoot(appRoutes);
base-module
@NgModule({
imports: [
CommonModule,
routing,
FormsModule
],
declarations: [
BaseComponent,
Child1Component,
Child2Component
],...
})
p.s
I've played a little with the Plunker routing demo of angular and indeed CrisisComponent
is being created on each navigation