I lazyload this feature module. It has different routes and the first one is the default route. As I need to setup a menu for this feature module I have tried to use the module constructor.
It works perfectly but is it semantically correct to use the module to setup data? I haven't see any example where someone used the same approach.
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import {OverviewComponent} from "@app/$/main/overview.component";
import {DetailComponent} from "@app/$/main/overview.component";
import {MenuService} from "@app/scheme/desk/menu/menu.service";
export const routes:Routes = [
{
path: "",
redirectTo: "overview"
},
{
path: "overview",
component : OverviewComponent,
},
{
path: "detail",
component : DetailComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
declarations: [OverviewComponent, DetailComponent],
exports: [RouterModule]
})
export class DeskModule {
constructor(private menuService:MenuService){
this.menuService.items = [{
label: 'Overview',
},
{
label: 'Detail',
}];
}
}
Regardless of if it is Angular or any other Class in any language the question
but is it semantically correct to use the module to setup data?
holds true for any class.
Now a Angular module
Then the problem boils down to
Should a class setup its data in the constructor ?
A general best practice for constructors is to do minimal setup that is required for its objects to make it easier to perform heavy work such as interacting with outside resources.
So as long as your "data setup" is really private and lightweight you should not perform this initialization IMO. This is just one case for not doing data initialization at the Module level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With