gpt4 book ai didi

Angular 8 在初始化之前无法访问 'PlanningModule'

转载 作者:行者123 更新时间:2023-12-03 13:39:12 25 4
gpt4 key购买 nike

我正在使用 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中的服务只有初始化.为什么我会收到此错误,我该如何解决?

应用路由.module.ts:
{
path: 'planning',
component: FullLayoutComponent,
children: Planning_ROUTES,
canActivate: [AuthGuard],
},

规划.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 { }

规划-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)');
}
}

规划组件.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 {}
}

规划路线.ts:
import { Routes, RouterModule } from '@angular/router';

export const Planning_ROUTES: Routes = [
{
path: '',
loadChildren: () => import('../../pages/planning/index/planning.module').then(m => m.PlanningModule)
},
];

最佳答案

当您在 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()绑定(bind) A_Component .一种选择可能是将服务移动到共享模块文件夹下。

关于Angular 8 在初始化之前无法访问 'PlanningModule',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com