自定义指令在角度模块内不起作用?

Kaz*_*azi 2 typescript angular-directive angular-module angular

在我的应用程序中,我有两个模块。IdentityModule and ServiceModule。它们以延迟加载技术加载。

现在我创建了一个IconSpacerDirective与 绑定的自定义指令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.
Run Code Online (Sandbox Code Playgroud)

这是我的identityRegistryModule

import { IconSpacerDirective } from '../shared/custom-directive/icon-spacer.directive';
@NgModule({
  declarations: [
    IconSpacerDirective
  ],
  imports: [
    IdentityRegistryRoutingModule,
    MaterialModule   

  ],
  providers: [
    IdentityService,
  ],
})
export class IdentityRegistryModule { }
Run Code Online (Sandbox Code Playgroud)

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 { }
Run Code Online (Sandbox Code Playgroud)

我如何使用IconSpacerDirective我的内部identityRegistryModule

Sam*_*ann 7

IconSpacerDirective如果您想在 theAppModule和 the中使用IdentityRegistryModule,则需要为该指令创建一个单独的模块,然后可以将其导入到需要的模块中。

@NgModule({
  declarations: [IconSpacerDirective],
  exports: [IconSpacerDirective]
})
export class IconSpacerModule { }
Run Code Online (Sandbox Code Playgroud)
@NgModule({
  imports: [IconSpacerModule]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
@NgModule({
  imports: [IconSpacerModule]
})
export class IdentityRegistryModule { }
Run Code Online (Sandbox Code Playgroud)