I have a module that has some routes:
export const routes: Routes = [
{
path: '/some-util-path',
component: SomeUtilComponent
}
];
@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule.forRoot(routes)
],
declarations: [
// --- some components ---
],
providers: [
// --- some services ---
],
exports: [
// --- some exports ---
],
})
export class MyUtilModule {
}
When I import this module in the app.module (root module) this works fine, but when I import it (as well) in a lazy-loaded module I get
Error: RouterModule.forRoot() called twice.
Lazy loaded modules should use RouterModule.forChild() instead.
How can I configure this Util Module to have the routes loaded as forRoot and forChild depending on its use-case?
I think I might split out the routes and 'SomeUtilComponent' to another Module that I only load in the app.module, but I'm interested in knowing if it is possible just with one module and conditional logic.
I was reading about the static forRoot and forChild methods, but I do not know how to arrange imports, as you can only specify providers.