angular - 自定义指令在角度模块中不起作用?
问题描述
在我的应用程序中,我有两个模块。IdentityModule and ServiceModule
. 它们作为延迟加载技术加载。
现在我创建了一个IconSpacerDirective
与 .bind 绑定的自定义指令app.module.ts
。它在我的app.component.ts
.
但它在IdentityModule
. 我有这个错误:
ERROR Error: Uncaught (in promise): Error: Type IconSpacerDirective is part of the declarations of 2 modules: AppModule and IdentityRegistryModule! Please consider moving IconSpacerDirective to a higher module that imports AppModule and IdentityRegistryModule. You can also create a new NgModule that exports and includes IconSpacerDirective then import that NgModule in AppModule and IdentityRegistryModule.
Error: Type IconSpacerDirective is part of the declarations of 2 modules: AppModule and IdentityRegistryModule! Please consider moving IconSpacerDirective to a higher module that imports AppModule and IdentityRegistryModule. You can also create a new NgModule that exports and includes IconSpacerDirective then import that NgModule in AppModule and IdentityRegistryModule.
这是我的identityRegistryModule
:
import { IconSpacerDirective } from '../shared/custom-directive/icon-spacer.directive';
@NgModule({
declarations: [
IconSpacerDirective
],
imports: [
IdentityRegistryRoutingModule,
MaterialModule
],
providers: [
IdentityService,
],
})
export class IdentityRegistryModule { }
我app.module.ts
的如下:
import { IconSpacerDirective } from './shared/custom-directive/icon-spacer.directive';
@NgModule({
declarations: [
AppComponent,
MainNavComponent,
SideNavComponent,
HeaderComponent,
HomeComponent,
IconSpacerDirective,
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule,
FlexLayoutModule,
BrowserAnimationsModule,
LayoutModule,
MaterialModule,
OAuthModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent],
//exports: [IconSpacerDirective]
})
export class AppModule { }
我怎样才能使用IconSpacerDirective
我的内部identityRegistryModule
解决方案
如果您想IconSpacerDirective
在 theAppModule
和中同时使用IdentityRegistryModule
,那么您需要为该指令创建一个单独的模块,然后您可以在需要它的模块中导入该模块。
@NgModule({
declarations: [IconSpacerDirective],
exports: [IconSpacerDirective]
})
export class IconSpacerModule { }
@NgModule({
imports: [IconSpacerModule]
})
export class AppModule { }
@NgModule({
imports: [IconSpacerModule]
})
export class IdentityRegistryModule { }
推荐阅读
- kubernetes - 在 Grafana 上使用 Prometheus 获取 Pod 的部署年龄或上次重启时间
- python - R unnest_token() 与 Python pandas str.split() 中的正则表达式行为
- join - Pyspark - 如何根据查找表重命名列名?
- jquery - Kendo Grid Detail Init 事件未在 IE 浏览器中触发
- sql - MariaDB 列按小时分组,行按标题分组
- c# - Linq 到对象,列表
,其中项目由列表组成 - javascript - 使用 Date.now() 的 javascript 中的 1 秒间隙
- java - java源文件的编码
- javascript - 在 React Js 中使用多个复选框过滤列表
- nativescript - 如何在 nativescript 中进行水平轮播布局?