简体   繁体   English

无法从其他NgModule的惰性模块中加载指令

[英]Can't load Directive in lazy module from other NgModule

I am having a problem loading a Directive and getting the dreaded Can't bind to 'hapPluginSlot' since it isn't a known property of 'ng-container'. 我在加载指令时遇到问题,并且使可怕的对象Can't bind to 'hapPluginSlot' since it isn't a known property of 'ng-container'.

In my project I have the following setup 在我的项目中,我有以下设置

app
  - shared
      shared.module
      - directives
          directives.module
          plugin-slot.directive
  - protected
      protected.module
      - home
          home.module (lazy loaded)
          home.component

An I have the following code in the respective files 我在相应文件中有以下代码

plugin-slot.directive 插件,slot.directive

@Directive({
  selector: '[hapPluginSlot]'
})
export class PluginSlotDirective {

  @Input() name: string;
  @Input() type: string;

  constructor() {
    console.log(this.name, this.type);
  }

}

directives.module directives.module

@NgModule({
  imports: [
    /* Angular Imports */
    CommonModule
  ],
  declarations: [
    PluginSlotDirective
  ],
  exports: [
    PluginSlotDirective
  ]
})
export class DirectivesModule { }

shared.module shared.module

@NgModule({
  imports: [
    /* Angular Imports */
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    ...
    /* Application Imports */
    PipesModule,
    DirectivesModule,
    ComponentsModule
  ],
  declarations: [
  ],
  exports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    PipesModule,
    DirectivesModule,
    ComponentsModule,
    ...
  ]
})
export class SharedModule { }

I am importing SharedModule into my home.module as below 我将SharedModule导入到home.module ,如下所示

@NgModule({
  imports: [
    SharedModule, //<--- shared module imported here
    HomeRouterModule
  ],
  declarations: [
    HomeComponent
  ]
})
export class HomeModule { }

And using the directive in the component template as below 并在组件模板中使用指令,如下所示

...
<mat-nav-list>
  <ng-container [hapPluginSlot] type="route">
  </ng-container>
</mat-nav-list>
...

And finally get to the question, I have checked and checked over this code and I am 100% certain that I have all the imports and exports in all the right places. 终于搞定了这个问题,我已经确认并检查了这些代码,我可以肯定100%,我有所有的importsexports在所有适当的地方。 Why am I getting this error? 为什么会出现此错误?

I have even tried stripping back to just load the directive in the AppModule and AppComponent thinking it may be something to do with lazy loaded modules, I am clearly missing something but I cannot see the wood for the trees as I have been staring at this for what feels like an eternity. 我什至尝试剥离以仅将指令加载到AppModuleAppComponent以为这可能与延迟加载的模块有关,我显然缺少了一些东西,但是由于一直盯着它,所以看不到树木的木头。感觉像是永恒。

I anyone can spot the problem then please, for the sake of the children, please point it out to me? 我谁能发现问题,然后,为了孩子们,请向我指出?

It's a syntax issue, first solution is to use your directive like this : 这是一个语法问题,第一个解决方案是使用您的指令,如下所示:

<ng-container hapPluginSlot type="route"></ng-container>

You could change the directive declaration to use a more compact syntax : 您可以更改指令声明以使用更紧凑的语法:

@Directive({
  selector: '[hapPluginSlot]'
})
export class PluginSlotDirective {
   ...
   @Input('hapPluginSlot') type: string;

And call it like this : 并这样称呼它:

<ng-container [hapPluginSlot]="route"></ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8 延迟加载模块 - 无法导航 - Angular 8 Lazy Load Module - Can't Navigate 如何在延迟加载的 ngModule 中通过 shared.module 加载服务,但 forRoot() 仅实例化一次 - How to load a service through shared.module in a lazy loaded ngModule only instantiated once though forRoot() 如何在没有 ngModule 的情况下使用 angular 加载惰性组件? - how to load lazy component with angular without ngModule? 我如何将延迟加载模块中的组件用作其他未延迟加载的组件中的子组件? - How can i use component from lazy loaded module as child in other component which is not lazy loaded? 组件无法从延迟加载的模块中找到提供程序 - component can't find provider from lazy-loaded module 从不同的路径延迟加载相同的模块 - Lazy load same module from different paths Angular2 RC5路由器找不到模块(延迟加载) - Angular2 RC5 router can't find module (lazy load) Angular 2 Lazy在具有角度路由器的NPM模块中加载NgModule - Angular 2 Lazy loading a NgModule in a NPM module with angular router Angular Nativescript 无法在惰性模块中导航 - Angular Nativescript can't navigate in lazy module 由模块'AppModule'导入的意外指令'LoginComponent'。请添加@NgModule注释 - Unexpected directive 'LoginComponent' imported by the module 'AppModule'. Please add a @NgModule annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM