Angular <custom-component> 不适用于延迟加载的模块

Hese

我创建了一个可以在 app.component 上正常工作的自定义组件,但是当<custom-component>在延迟加载模块中使用它时会出现错误:

Error: Template parse errors:
'my-component' is not a known element:
1. If 'my-component' is an Angular component, then verify that it is part of this module.
2. If 'my-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

我正在使用 Angular 4.0.0/CLI

代码:

app.module.ts

...

import { LoaderComponent } from './shared/components/list-components/loader/loader.component';

@NgModule({
  declarations: [
    AppComponent,
    LoaderComponent <- declared here because I need the component in multiple modules
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

loader.component.ts

import { Component, OnInit, Input, Output } from '@angular/core';

@Component({
  selector: 'my-component',
  templateUrl: './loader.component.html',
  styleUrls: ['./loader.component.css']
})
export class LoaderComponent implements OnInit {
  @Input() type:any;

  constructor() { }

  ngOnInit() {
  }

}

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [

  { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' },

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

应用程序组件.html

<router-outlet></router-outlet>
<my-component></my-component> <-- works here

和lazy.component.html

<p>
  this module is lazy loaded
  <my-component ></my-component> <- not working
</p>

有任何想法吗?

* 更新 *

我创建了一个 SharedModule,它声明了 LoaderComponent。SharedModule 被导入到每个需要 LoaderComponent 的模块中。作品!

德米特里古巴

您需要创建 LoaderModule:

@NgModule({
  imports: [],
  exports: [LoaderComponent],
  declarations: [LoaderComponent],
  providers: [],
})
export class LoaderModule {
}

然后将此模块添加到 app.module

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    LoaderModule,
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
  })
  export class AppModule { }

确保您也将 LoaderModule 添加到 LazyModule

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

angular 4 的模块延迟加载不适用于 Laravel 项目

来自分类Dev

Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块

来自分类Dev

在Angular 6中使用延迟加载时,路由不适用于其他路由

来自分类Dev

angular 6 共享模块不适用于其他模块

来自分类Dev

Angular不适用于使用jQuery加载功能加载的div

来自分类Dev

ngAnimate不适用于Angular 1.2.16

来自分类Dev

Angular代码不适用于Express

来自分类Dev

angular js模块中的控制器功能不适用于删除操作

来自分类Dev

Node.js核心模块fs不适用于webpack和angular2 cli

来自分类Dev

Angular 9,滚动恢复/锚定滚动不适用于异步数据加载

来自分类Dev

Angular ng-show不适用于初始页面加载

来自分类Dev

Angular Universal不适用于Angular Google Maps

来自分类Dev

Angular 7:Rxjs行为主题订阅不适用于页面重新加载,但可用于路线导航

来自分类Dev

Angular Translate指令不适用于ngMessage

来自分类Dev

锚标记不适用于Angular UI-Router

来自分类Dev

Highlight.js不适用于Angular 2

来自分类Dev

Angular基础教程不适用于Windows吗?

来自分类Dev

触摸事件不适用于Angular中的Google Maps

来自分类Dev

崩溃过渡不适用于angular的UI引导程序

来自分类Dev

Angular JS验证不适用于Bootstrap的预输入

来自分类Dev

Angular指令不适用于更改值

来自分类Dev

Angular的ng include指令不适用于预期的链接

来自分类Dev

jQuery Xeditable不适用于Angular2

来自分类Dev

Angular.js set href不适用于指令

来自分类Dev

Angular2 useAsDefault不适用于子路由

来自分类Dev

ngStyle不适用于angular2中的!important

来自分类Dev

angular2 canActivate()不适用于Observable响应

来自分类Dev

Angular 10不适用于Windows 10

来自分类Dev

Angular-表排序不适用于嵌套对象

Related 相关文章

  1. 1

    angular 4 的模块延迟加载不适用于 Laravel 项目

  2. 2

    Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块

  3. 3

    在Angular 6中使用延迟加载时,路由不适用于其他路由

  4. 4

    angular 6 共享模块不适用于其他模块

  5. 5

    Angular不适用于使用jQuery加载功能加载的div

  6. 6

    ngAnimate不适用于Angular 1.2.16

  7. 7

    Angular代码不适用于Express

  8. 8

    angular js模块中的控制器功能不适用于删除操作

  9. 9

    Node.js核心模块fs不适用于webpack和angular2 cli

  10. 10

    Angular 9,滚动恢复/锚定滚动不适用于异步数据加载

  11. 11

    Angular ng-show不适用于初始页面加载

  12. 12

    Angular Universal不适用于Angular Google Maps

  13. 13

    Angular 7:Rxjs行为主题订阅不适用于页面重新加载,但可用于路线导航

  14. 14

    Angular Translate指令不适用于ngMessage

  15. 15

    锚标记不适用于Angular UI-Router

  16. 16

    Highlight.js不适用于Angular 2

  17. 17

    Angular基础教程不适用于Windows吗?

  18. 18

    触摸事件不适用于Angular中的Google Maps

  19. 19

    崩溃过渡不适用于angular的UI引导程序

  20. 20

    Angular JS验证不适用于Bootstrap的预输入

  21. 21

    Angular指令不适用于更改值

  22. 22

    Angular的ng include指令不适用于预期的链接

  23. 23

    jQuery Xeditable不适用于Angular2

  24. 24

    Angular.js set href不适用于指令

  25. 25

    Angular2 useAsDefault不适用于子路由

  26. 26

    ngStyle不适用于angular2中的!important

  27. 27

    angular2 canActivate()不适用于Observable响应

  28. 28

    Angular 10不适用于Windows 10

  29. 29

    Angular-表排序不适用于嵌套对象

热门标签

归档