You need to enable JavaScript to run this app. Angular英雄之旅开发遇NG8002:hero非app-hero-detail已知属性报错
优惠活动
大模型
产品
解决方案
定价
更多

Angular英雄之旅开发遇NG8002:hero非app-hero-detail已知属性报错

问题核心成因

报错NG8002的本质是模板中使用的自定义组件标签,和组件自身定义的选择器字符串不匹配,导致Angular完全无法识别对应组件,自然无法解析组件上的输入属性绑定。
两个字符串的拼写偏差点很明确:

  • 你在heroe-detail.component.ts中定义的组件选择器为 app-heroe-detail(在单词hero后多写了字母e,拼写为西语“heroe”)
  • 你在heroes.component.html中实际引用的组件标签为 <app-hero-detail>(拼写为标准英文hero,没有多余的字母e)
    编译器解析模板时,找不到和app-hero-detail匹配的已注册组件,会把该标签当作未知的原生HTML元素处理,因此判定其上的[hero]绑定没有对应属性,抛出编译错误。移除该标签后不存在未知元素校验,应用自然可以正常运行。
修复操作

二选一即可,优先对齐官方教程的标准拼写:

  1. 修改组件选择器,和模板引用保持一致
    打开heroe-detail.component.ts,调整@Component装饰器中的selector值,去掉多余的字母e:
    @Component({
      // 原错误写法:selector: 'app-heroe-detail'
      selector: 'app-hero-detail',
      templateUrl: './heroe-detail.component.html',
      styleUrls: ['./heroe-detail.component.scss']
    })
    
  2. 或者修改模板标签,和现有组件选择器保持一致
    打开heroes.component.html,把对应行的标签拼写改为和组件选择器匹配的内容:
    <!-- 原错误写法:<app-hero-detail [hero]="selectedHero"></app-hero-detail> -->
    <app-heroe-detail [hero]="selectedHero"></app-heroe-detail>
    
改完后仍报错的核对清单
  • 确认HeroeDetailComponent已经正确加入app.module.ts的declarations数组,你贴出的代码中这一步配置无问题。
  • 确认@Input装饰器已从@angular/core正确导入,且标注在组件的hero属性上,你贴出的组件代码这一步配置无问题。
  • 确认没有在其他模块重复注册同名选择器的组件,避免解析冲突。

内容的提问来源于stack exchange,提问作者bfvanrooyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 21:15:08