未捕获错误:模板解析错误:'component'不是已知元素:

时间:2017-12-11 04:52:45

标签: javascript angular angular2-template

我正面临角度4.4的问题,在我的应用程序中,我有分层视图。我在其中创建了一个

:=模块1 - >模块2 - >我的组件。

我已正确宣布一切。 ,但我仍然收到错误。

  1. 声明组件。
  2. 将其导入到其他模块中。
  3. 选择器名称相同。
  4. 模块2 中的
  5. 导出组件。
  6. 模块1
  7. 导入模块2。
  8. 可能有什么问题? 组件代码: 管理员 - >配置 - > myComponent的 //我的组件

    import { Component, OnInit, ViewChild, ChangeDetectorRef } from '@angular/core';
    @Component({
      selector: 'test-mycomponent',
      templateUrl: './mycomponent.component.html',
      styleUrls: ['./mycomponent.component.scss']
    })
    export class MyComponentComponent implements OnInit {
      @ViewChild('myComponentTable')
    
      constructor() {
        }
      ngOnInit() {
    //init functionality
    }
    }
    
    // configure module code 
    import { NgModule,Component } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule } from '@angular/forms';
    import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
    import { MyComponentComponent } from './mycomponent/mycomponent.component';
    @NgModule({
      imports: [
        CommonModule,
        WidgetsModule,
        FormsModule,
        NgbModule
      ],
      declarations: [
        MyComponent
      ],
      providers: [
      ],
      exports: [
        MyComponent
      ]
    })
    export class ConfigurationModule { }
    
    //Main module admin
    import { NgModule, Component } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { ConfigurationModule } from './configuration/configuration.module';
    import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
    import { MyComponentComponent } from './configuration/mycomponent/mycomponent.component';
    import { FormsModule } from '@angular/forms';
    @NgModule({
      imports: [
        CommonModule,
        NgbModule,
        FormsModule,
        ConfigurationModule
      ],
      declarations: [
      ],
      exports: [
         ]
    })
    export class AdminModule { }
    
    and I am calling that template in another file
    //Test file html 
        <ng-template>
              <test-mycomponent></test-mycomponent>
          </ng-template> 
    

1 个答案:

答案 0 :(得分:1)

您没有在声明和导出中声明正确的组件名称应该是MyComponentComponent:

import { MyComponentComponent } from './mycomponent/mycomponent.component';

declarations: [
    MyComponent //should be MyComponentComponent 
  ],
  providers: [
  ],
  exports: [
    MyComponent //should be MyComponentComponent 
  ]
相关问题