I am still getting used to Angular and how to properly structure different components and modules. I want to set my app up according to best practices and maximize efficiency. I currently have multiple modules. I have a "base" module that I want to navigate to, but depending on the URL, I want to incorporate components from other modules. Here is how my app-routing.module is currently set up:
const routes: Routes = [
{ path: '', component: BaseComponent },
{ path: 'featureone', loadChildren: () => import('./feature-one/feature-one.module').then(m => m.FeatureOneModule) },
{ path: 'featuretwo', loadChildren: () => import('./feature-two/feature-two.module').then(m => m.FeatureTwoModule) },
{ path: '**', redirectTo: '', pathMatch: 'full'}
];
I understand this routing is not set up properly, but I am not sure how I can properly set this up in the most efficient way.
Currently, if I navigate to ''
, it will load the BaseComponent as expected. If I add
<app-feature-one></app-feature-one>
or
<app-feature-two></app-feature-two>
to the BaseComponent template, it will throw an error like "Cannot access 'FeatureOneModule' before initialization"
Is there some way where I could keep routes such as 'featureone' and 'featuretwo' where it would navigate to the BaseComponent, and I could add logic to display <app-feature-one></app-feature-one>
or
<app-feature-two></app-feature-two>
and only load FeatureOneModule if I navigate to 'featureone' or FeatureTwoModule if I navigate to 'featuretwo'?