这个问题通常发生在使用 ngFor 循环渲染数据时。它表明在所提供的 'IStudent' 类型上不存在 'results' 属性。为了解决该问题,我们需要在 'IStudent' 类型中添加 'results' 属性,或者在遍历 'IStudent' 数组时更改变量为包含 'results' 属性的对象。以下是解决方式示例:
// IStudent 接口 interface IStudent { name: string; age: number; results: number[]; }
// 通过 .map() 更改对象中不包含 'results' 属性的变量 this.students = [ { name: '小明', age: 20 }, { name: '小红', age: 21 }, { name: '小刚', age: 19 } ].map((student: IStudent) => { return { ...student, results: [] // 添加 'results' 属性 }; });
// 在 ngFor 中更改变量到包含 'results' 属性的对象
{{ student.name }}
{{ student.age }}