【问题标题】:Uncaught Error: Template parse errors: 'component' is not a known element:未捕获的错误:模板解析错误:“组件”不是已知元素:
【发布时间】:2018-05-24 14:30:00
【问题描述】:

我在 angular 4.4 中遇到问题,在我的应用程序中,我有分层视图。我在其中创建了一个

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

我已经正确地声明了一切。 , 但我仍然遇到错误。

  1. 声明的组件。
  2. 已将其导入到其他模块中。
  3. 选择器名称相同。
  4. 模块 2导出组件。
  5. 模块 1导入模块 2。

有什么问题? 组件代码: 管理 -> 配置 -> 我的组件 //我的组件

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> 

【问题讨论】:

  • 你能添加代码吗..
  • @AmolRaje 请检查

标签: javascript angular angular2-template


【解决方案1】:

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

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

declarations: [
    MyComponent //should be MyComponentComponent 
  ],
  providers: [
  ],
  exports: [
    MyComponent //should be MyComponentComponent 
  ]

【讨论】:

  • 我已经在我的代码中纠正了这个问题,但仍然是同样的错误。
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 2018-01-24
  • 2018-10-11
  • 1970-01-01
  • 2020-04-19
相关资源
最近更新 更多