原始异常:无法读取属性'值'的未定义

ORIGINAL EXCEPTION: Cannot read property 'Value' of undefined

本文关键字:未定义 属性 异常 读取 原始      更新时间:2023-09-26

我收到这个错误cannot read property 'Value' of undefined,我做错了什么?

years: any[] = [];
ngOnInit() {
    for (let i = 1970; i <= new Date().getFullYear(); i++)
    {
        this.years.push({'Value': i});
    }
}

无法读取未定义的属性"Value">

很明显,你正试图在有.Value的东西上使用它。例如

this.years.push({'Value': i});
let year = undefined;
year.Value; // BANG

也许你想要这样的东西:

this.years.push({'Value': i});
let year = this.years[0];
year.Value; // Okay