【发布时间】:2021-07-27 13:53:44
【问题描述】:
我有一个问题,我有一个名为 CoreModule 的模块,它是在 AppModule 中导入的,我试图在某些子模块中使用这个模块,但是如果我在子模块中导入模块,它会给出这个当我转到该模块中的某个组件时出现错误CoreModule has already been loaded. Import Core modules in the AppModule only.。
如果我不在子模块中导入CoreModule,则会出现此错误:Can't bind to 'ngxHasAnyRole' since it isn't a known property of 'button'
我的AppModule 中的导入看起来像这样
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AppRoutingModule,
NbSidebarModule.forRoot(),
NbMenuModule.forRoot(),
NbDatepickerModule.forRoot(),
NbDialogModule.forRoot(),
NbWindowModule.forRoot(),
NbToastrModule.forRoot(),
CoreModule.forRoot(),
ThemeModule.forRoot(),
LoggerModule.forRoot({
level: NgxLoggerLevel.INFO,
enableSourceMaps: true
}),
],
我的CoreModule 看起来像这样
...
@NgModule({
imports: [
CommonModule,
],
exports: [
NbAuthModule,
HasRoleDirective,
HasAnyRoleDirective,
],
declarations: [HasAnyRoleDirective, HasRoleDirective],
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
throwIfAlreadyLoaded(parentModule, 'CoreModule');
}
static forRoot(): ModuleWithProviders<CoreModule> {
return {
ngModule: CoreModule,
providers: [
...NB_CORE_PROVIDERS,
],
};
}
这是我尝试使用来自CoreModule的指令的html部分
<button nbButton
(click)="new()"
[ngxHasAnyRole]="['ROLE_TEST', 'ROLE_ADMIN']"
class="table-button"
status="primary"
size="small">
NEW
<nb-icon icon="plus-outline"></nb-icon>
</button>
我不想在子模块中导入,因为这是在父模块中导入的。我该如何解决这个问题?
其他信息
当我在孩子中导入CoreModule 时执行ng serve,然后在ng serve 开启时将其删除。它有效,但如果我关闭并重新开始。它不会启动。
【问题讨论】: