gpt4 book ai didi

javascript - Angular 构造函数中未定义的“push”

转载 作者:行者123 更新时间:2023-12-01 00:50:30 25 4
gpt4 key购买 nike

仍在学习 Angular。当尝试在构造函数内填充数字数组时看到这一点

Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined

尝试像这样在我的构造函数中填充数组

export class RegisterComponent implements OnInit {
@Output() cancelRegister = new EventEmitter();
user: User;
registerForm: FormGroup;
bsConfig: Partial <BsDatepickerConfig> ;
emailErrorMessage: string;
userErrorMessage: string;
currentYear: number = new Date().getFullYear();
startYear = this.currentYear - 120;
years: number[];


constructor(private authService: AuthService, private router: Router,
private alertify: AlertifyService, private fb: FormBuilder) {
for (let i = this.currentYear; i > this.startYear; i--) {
this.years.push(i);
}
}

最佳答案

您必须初始化类的属性。

export class RegisterComponent implements OnInit {
...
years: number[] = [];
...

关于javascript - Angular 构造函数中未定义的“push”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57023031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com