【问题标题】:Custom components not loading Angular 8自定义组件未加载 Angular 8
【发布时间】:2019-12-19 04:22:14
【问题描述】:

父组件.html

 <div ["child-component"]> </div>

子组件.html

<div> This is a Child Component </div>

子组件.ts

 import { Component } from '@angular/core';
 import { FormBuilder } from '@angular/forms';

 @Component({
  selector: 'child-component',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
 })
  export class ChildComponent{
  constructor(private formBuilder: FormBuilder) {}
  }

问题:ParentComponent 加载时不渲染子组件。

错误:控制台无错误,子组件中未命中调试点。

我尝试在 ParentComponentMoudle.ts 文件中包含 ChildComponentModule。

注意:我正在从 Angular 4 迁移到 8。

总结:我要做的就是在页面上加载一个非常基本的自定义组件

【问题讨论】:

  • 嗨,伙计,你能提供更多代码吗?只有你的书面解释,人们什么也做不了。问候
  • 你能提供你ChildComponent.ts文件的前20行吗?应该有一个叫做selector的属性,它定义了你如何绑定到html中的组件。

标签: angular angular8


【解决方案1】:

如果每个组件都有单独的模块,则在父模块中添加子模块引用并在父模块中导入子组件。然后在 Grand Parent Module 中添加 Parent Module 引用并导入 Parent Component。然后,在你的 GrandParent.html

<ParentComponent> </ParentComponent>

像上面一样添加

【讨论】:

  • 它没有给出任何错误并且没有加载子组件
【解决方案2】:

在父组件的视图(html)中,使用子组件的选择器而不是类名:

<child-component></child-component>

【讨论】:

  • 我也是这样使用的
  • 我的错。修复。像这样检查一下。
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多