I am using the apex theme from themeforest. I am trying to acces the page: /planning
However, the console shows a error:
core.js:6014 ERROR Error: Uncaught (in promise): ReferenceError: Cannot access 'PlanningModule' before initialization
ReferenceError: Cannot access 'PlanningModule' before initialization
at Module.PlanningModule (planning.component.ts:12)
at planning-layout.routes.ts:6
I don't have double initialization of the component nor the module. Only initialization is the service in app.module
. Why did I get this error and how can I solve it?
App-routing.module.ts:
{
path: 'planning',
component: FullLayoutComponent,
children: Planning_ROUTES,
canActivate: [AuthGuard],
},
Planning.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {PlanningComponent} from './planning.component';
import {FormsModule} from '@angular/forms';
import {DragulaModule} from 'ng2-dragula';
import {Ng2SmartTableModule} from 'ng2-smart-table';
import {NgSelectModule} from '@ng-select/ng-select';
import {PlanningRoutingModule} from './planning-routing.module';
@NgModule({
declarations: [
PlanningComponent
],
imports: [
CommonModule,
FormsModule,
DragulaModule.forRoot(),
Ng2SmartTableModule,
NgSelectModule,
PlanningRoutingModule
]
})
export class PlanningModule { }
Planning-routing.module.ts:
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {PlanningComponent} from './planning.component';
const routes: Routes = [
{
path: '',
component: PlanningComponent,
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class PlanningRoutingModule {
constructor() {
console.log('hier komt die(niet)');
}
}
PlanningComponent.ts:
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {PlanningService} from './planning.service';
@Component({
selector: 'app-planning',
templateUrl: './planning.component.html',
styleUrls: ['./planning.component.scss'],
})
export class PlanningComponent implements OnInit {
constructor(private router: Router,
private planningService: PlanningService) {}
ngOnInit(): void {}
}
Planning-routes.ts:
import { Routes, RouterModule } from '@angular/router';
export const Planning_ROUTES: Routes = [
{
path: '',
loadChildren: () => import('../../pages/planning/index/planning.module').then(m => m.PlanningModule)
},
];
This happens when you have a circular dependency between 1 or more Angular Modules. I found it happens even if dependency is indirect, such as a service import. I'm assuming this has to do with initialization order.
For example -
This component has a dependency on a service defined in another module's subdirectory -
// declared in A_Module
export class A_Component implements OnInit {
//...
constructor(
private service: B_Service) {} // <-- dependency on B_Module??
}
// B_Module
@NgModule({
imports: [
CommonModule,
A_Module // <-- dependency
],
exports: [],
declarations: [...],
providers: []
})
export class B_Module { }
// In B_Module sub-directory
@Injectable({providedIn: 'root'})
export class B_Service {}
To resolve, I used an Input()
binding in A_Component
. One option might be to move the service under a shared module folder.
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