You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular组件传值报错求助:NG8002及@Input未识别问题

解决Angular组件间数据传递的编译错误

第一步:修复@Input未导入的错误

在src/app/shared/components/select/select.component.ts文件顶部,添加Input的导入语句:

import { Component, Input } from '@angular/core';

原因:你在组件中使用了@Input()装饰器,但未从Angular核心库导入它,TypeScript无法识别该装饰器,因此触发TS2552错误。

第二步:确保app-select组件在模块中正确声明与导出

  1. 找到SelectComponent所属的模块(通常是SharedModule),在模块的declarations数组中添加该组件,并在exports数组中导出(供其他模块使用):
@NgModule({
  declarations: [
    SelectComponent
    // 其他组件
  ],
  exports: [
    SelectComponent
  ]
})
export class SharedModule { }
  1. 在包含AllProductsComponent的模块(如ProductsModule)中,导入上述SharedModule:
@NgModule({
  imports: [
    SharedModule
    // 其他导入模块
  ],
  declarations: [
    AllProductsComponent
    // 其他组件
  ]
})
export class ProductsModule { }

原因:Angular要求组件必须在模块中声明,才能在模板中识别组件标签及其输入属性,否则会触发NG8002错误,提示无法绑定data属性。

验证修复

完成上述操作后,重新执行ng serve,编译错误将被消除,此时AllProductsComponent的categories数据可通过[data]="categories"成功传递给SelectComponent的data输入属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 07:15:04