gpt4 book ai didi

angular - 自定义指令在 Angular 模块内不起作用?

转载 作者:行者123 更新时间:2023-12-02 00:10:05 26 4
gpt4 key购买 nike

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

现在我已经创建了一个与 app.module.ts 绑定(bind)的自定义指令 IconSpacerDirective。它在我的 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 { }

如何在我的 identityRegistryModule 中使用 IconSpacerDirective

最佳答案

如果你想在 AppModuleIdentityRegistryModule 中使用 IconSpacerDirective,那么你需要为该指令创建一个单独的模块然后您可以在需要的模块中导入它。

@NgModule({
declarations: [IconSpacerDirective],
exports: [IconSpacerDirective]
})
export class IconSpacerModule { }
@NgModule({
imports: [IconSpacerModule]
})
export class AppModule { }
@NgModule({
imports: [IconSpacerModule]
})
export class IdentityRegistryModule { }

关于angular - 自定义指令在 Angular 模块内不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59358037/

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