javascript - “app-element”不是已知的 Angular 元素

标签 javascript angular

我尝试在 contact-me 组件中使用 contact-form 组件的选择器,但它给出错误“'app-contact-form'不是已知元素”?我的代码如下:

联系我.module.ts

        import { NgModule } from '@angular/core';
        import { CommonModule } from '@angular/common';
        import { ContactMeComponent } from './contact-me.component';
        import { ContactFormComponent } from './contact-form/contact-form.component';
        import { ContactFormModule } from './contact-form/contact-form.module';


       @NgModule({
            declarations: [ContactMeComponent, ContactFormComponent],
            imports: [
               CommonModule,
               ContactFormModule
            ],
            exports: [ContactMeComponent, ContactFormComponent]
         })
         export class ContactMeModule { }

联系表单.module.ts

        import { NgModule } from '@angular/core';
        import { CommonModule } from '@angular/common';
        import { ContactFormComponent } from './contact-form.component';
        import { FormsModule, ReactiveFormsModule } from '@angular/forms';



        @NgModule({
           declarations: [ContactFormComponent],
           imports: [
            CommonModule,
            FormsModule,
            ReactiveFormsModule
          ],
           exports: [ContactFormComponent]
        })
         export class ContactFormModule { }

联系我.component.html

         <app-contact-form></app-contact-form>

contact-form.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-contact-form',
  templateUrl: './contact-form.component.html',
  styleUrls: ['./contact-form.component.scss']
})
export class ContactFormComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  contactForm = new FormGroup({
    projectTitle: new FormControl(''),
    fullName: new FormControl(''),
    emailAddress: new FormControl(''),
    phoneNumber: new FormControl('')
  });

}

contact-me 是应用模块中的路由之一。

这是我的控制台错误:

src/app/contact-me/contact-me.component.html:1:4 中出现错误 - 错误 NG8001:“app-contact-form”不是已知元素: 1. 如果“app-contact-form”是 Angular 组件,则验证它是否是该模块的一部分。 2. 如果“app-contact-form”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。

1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/contact-me/contact-me.component.ts:5:16 5 templateUrl: './contact-me.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 组件ContactMeComponent模板出错。

** Angular Live Development Server 正在监听 localhost:4200,打开浏览器 http://localhost:4200/ **

还需要什么或者我做错了什么?

最佳答案

contact-me.module.ts 中的 declarationsexports 中删除 ContactFormComponent 仅此而已。

说明:

您正在使用app-contact-form,它位于contact-form.module.ts内。因此,您必须从那里导出它才能在另一个模块中使用它。

然后,在需要 ContactFormComponent 的地方导入 ContactFormModule。在您的案例中,导入 contact-me.module.ts

关于javascript - “app-element”不是已知的 Angular 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61201371/

相关文章:

twitter-bootstrap - Angular 4 : How to include Bootstrap?

javascript - 媒体源缓冲区数据去哪里了?

javascript - 条件必填字段

javascript - 如何解决DataTables表头列宽和表体列宽有时不匹配的问题?

angular - 在 Angular 2 组件模板中嵌入小部件

javascript - 如何将对象从对象中的数组推送到另一个数组?

javascript - 带有 ng-change 的 Angularjs slider

javascript - Angular Controller 可以绑定(bind)到网页的多个部分吗?

javascript - 创建由多个组件组成的表

html - Angular 可滚动的垫选择列表?