0

Im getting this error but I don't know why, since this core module is being imported only once

The code App.Module

  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    AppRoutingModule,
    SharedModule,
    CoreModule
    ],
  providers: [
    DataService, 
    AuthService,
    ShoppingCartService,
    ProductService,
    HttpClientModule

The appRouting:

const adminModule = ()=> import('./admin/admin.module').then(m=>m.AdminModule);
const coreModule = ()=> import('./core/core.module').then(c=>c.CoreModule);

const routes: Routes = [
  {
    path: '', component:ShellComponent,
    children: [
      {path: 'detail/:id', component: DetailComponent},
      {path: 'admin', loadChildren:adminModule},
      {path: 'home', loadChildren:coreModule},
    ]
  },
  {path: 'login', component: LoginComponent},
  {path:'edit/:id', component:RegisterComponent},
  {path:'register', component: RegisterComponent} 
];

I get this error when I navigate to home page, as I have this constructor in the core module

@NgModule({
  declarations: [
    MoviesComponent,
    DetailNoRoutingComponent,
    HomeComponent,
    ProductCardComponent,
    ProductFilterComponent
  ],
  imports: [
    SharedModule,
    coreRoutingModule
  ]
})
export class CoreModule { 

  constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
    if (parentModule) {
      throw new Error('CoreModule has already been loaded. You should only import Core modules in the AppModule only.');
    }
  }

Can anyone help me out? Im not understanding whats goin on, maybe something Im missing in the imports or the logic is messed up

4
  • You are importing it twice. First in app.module (imports) and then lazy-loading it when navigating to 'home'. Or is this another CoreModule? Commented Jun 17, 2020 at 22:22
  • no, its just one core module...Im learning how to do proper routing configuration and as I understand there are plent of ways to achieve it´ Commented Jun 17, 2020 at 23:05
  • If it's just one CoreModule you are importing it twice. You have to decide: 1. Create a HomeModule - replace it with coreModule in appRouting and import the CoreModule just once in app.module (imports) OR 2. Leave coreModule (lazy-loding) in appRouting and remove the import from app.Module Commented Jun 17, 2020 at 23:15
  • I think I understand your point, but can you chow that in code?One of the options that you consider the best aproach,,,thank in advance Commented Jun 17, 2020 at 23:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.