angular - 获取 "' 组件'不是已知元素“angular2 中的错误

标签 angular typescript

我的 Angular 2 应用程序中有两个组件。 AppComponent 使用 MyComponent:

app.component.ts :

import { Component } from '@angular/core';
import { MyComponent } from './comp1/app.component2';

@Component({
    selector: 'my-app',
    directives: [MyComponent],
    template: `
      <h1>My First Angular 2 App</h1>  <h2>Welcome all!!!!</h2>

      <a href="http://google.co.in/">Google</a><br/><a href="http://www.facebook.com/">facebook</a><br/><a href="http://www.twitter.com">twitter</a>

      <ul><li *ngFor="let name of names">{{name}}</li></ul>

      <my-comp></my-comp>`
})
export class AppComponent {
    names: String[];
    constructor() {
        this.names = ["Praveen", "Naveen", "Nandini"];
    }
}

app.component2.ts :

import {Component} from '@angular/core'
@Component({
    selector: 'my-comp',
    templateUrl: 'app/comp1/my-component.html'
})
export class MyComponent {
    msg: String = "My Component ---- hurrayyyyyy!!!!!!";
}

app.module.ts :

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
@NgModule({
    imports: [BrowserModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {}

当我运行它时,出现以下错误:

Error in console :

Unhandled Promise rejection: Template parse errors:
'my-comp' is not a known element:
1. If 'my-comp' is an Angular component, then verify that it is part of this module.
2. If 'my-comp' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. (".com">twitter</a>
              <ul><li *ngFor="let name of names">{{name}}</li></ul>
              [ERROR ->]<my-comp></my-comp>
"): AppComponent@4:14 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:
'my-comp' is not a known element:
1. If 'my-comp' is an Angular component, then verify that it is part of this module.
2. If 'my-comp' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. (".com">twitter</a>
              <ul><li *ngFor="let name of names">{{name}}</li></ul>
              [ERROR ->]<my-comp></my-comp>
"): AppComponent@4:14 {stack: (...), message: "Template parse errors:↵'my-comp' is not a known el…RROR ->]<my-comp></my-comp>↵"): AppComponent@4:14"}

我该如何解决?

最佳答案

您需要在app.module 文件中导入并声明MyComponent,如下所示,

注意:同时从 AppComponent

中删除 directives : [MyComponent]
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { MyComponent } from './comp1/app.component2';  //<----here

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent,MyComponent],           //<-----here
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

关于angular - 获取 "' 组件'不是已知元素“angular2 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39437985/

相关文章:

javascript - 将前缀添加到 Angular 开发服务器 url

javascript - Angular 和 Firebase - 如何显示错误消息

angular - 在 Angular4 库上运行 ngc 时,TsickleCompilerHost 出错

typescript - Describe is not defined exception on Typescript, Mocha 和 VSCode

javascript - Gulpfile 仅高效编译已更改的 TypeScript 文件

html - CSS Flex 将元素堆叠在一起

angular - 以 Angular 2 react 形式动态删除表单控件

javascript - 动画化动态 Angular 组件的移除?

reactjs - 由于 lib.dom.d.ts 和 react-native 之间存在重复的标识符,TypeScript 编译失败

typescript - 如何在 TypeScript 中获取对象中所有键值对的联合类型?