我正在使用themeforest中的apex主题。我试图访问该页面:/planning
但是,控制台显示错误:
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
我没有组件或模块的双重初始化。 app.module
中的服务仅是初始化。为什么会出现此错误,我该如何解决?
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)
},
];
答案 0 :(得分:3)
当您在1个或多个Angular模块之间具有循环依赖关系时,会发生这种情况。 我发现即使依赖是间接的,例如服务导入,它也会发生。我假设这与初始化顺序有关。
例如-
此组件依赖于另一个模块的子目录中定义的服务-
// 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 {}
为解决此问题,我在Input()
中使用了A_Component
绑定。一种选择是将服务移动到共享模块文件夹下。