【问题标题】:ORIGINAL EXCEPTION: Cannot read property 'Value' of undefined原始异常:无法读取未定义的属性“值”
【发布时间】:2016-09-21 07:51:36
【问题描述】:

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

years: any[] = [];

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

【问题讨论】:

  • 向我们展示您从对象中获取值的代码。异常应该向您显示发生错误的行。
  • @jcubic,就是这样,我想创建一个对象并像这样使用year.Value
  • 这条线看起来很奇怪 years: any[] = []; 它应该是 var years = [];years: [], 如果它在对象内部。
  • @jcubic 这是类型化数组的 angular2/typescript 初始化。该行是正确的AFAIK。
  • @BorisShchegolev 没有注意到这个问题有标签 typescript。

标签: javascript arrays angular typescript


【解决方案1】:

无法读取未定义的属性“值”

很明显,您正试图在确实有它的东西上使用.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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2019-09-14
    • 2020-03-26
    • 2022-08-18
    相关资源
    最近更新 更多