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

Angular父子组件传值报错:Property 'pod'不存在于ProductListComponent

解决Angular父子组件传值时的*ngFor变量报错问题
  • 错误原因:模板中*ngFor="Let pod of products"的Let是大写的,Angular模板指令要求循环变量声明必须用小写的let。大写的Let会被识别为要访问组件类的pod属性,但你的ProductListComponent类里并没有定义这个属性,因此触发报错。

  • 修正步骤:

    1. 将模板里的大写Let改为小写let,正确的循环代码如下:
    <app-product *ngFor="let pod of products" [product]="pod"></app-product>
    
    1. 额外确认ProductListComponent中的products数组已正确初始化(避免循环无数据):
    // product-list.component.ts
    import { Component } from '@angular/core';
    import { Product } from './product.model';
    
    @Component({
      selector: 'app-product-list',
      templateUrl: './product-list.component.html'
    })
    export class ProductListComponent {
      // 示例初始化产品数据
      products: Product[] = [
        { id: 1, name: '测试产品1', price: 89 },
        { id: 2, name: '测试产品2', price: 129 }
      ];
    }
    
    1. 确保子组件的@Input绑定正确:
    // product.component.ts
    import { Component, Input } from '@angular/core';
    import { Product } from './product.model';
    
    @Component({
      selector: 'app-product',
      templateUrl: './product.component.html'
    })
    export class ProductComponent {
      @Input() product!: Product;
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 00:37:09