Javascript ngOnInit()之外的角度访问阵列
中有一个称为数字的数组Javascript ngOnInit()之外的角度访问阵列,javascript,angular,angular-directive,Javascript,Angular,Angular Directive,中有一个称为数字的数组 export class MyphotographsComponent implements OnInit { numbers =[] } 在“numbers”数组之后,我有onNgInit(){}。在这方面有一个功能。从这个函数中,我想将一个变量推送到NgOnInit之外的数组中。我试过“这个。数字。推[我]”,这种力量不起作用。需要帮忙吗 下面是我的示例代码 export class MyphotographsComponent implements OnI
export class MyphotographsComponent implements OnInit {
numbers =[]
}
在“numbers”数组之后,我有onNgInit(){}。在这方面有一个功能。从这个函数中,我想将一个变量推送到NgOnInit之外的数组中。我试过“这个。数字。推[我]”,这种力量不起作用。需要帮忙吗
下面是我的示例代码
export class MyphotographsComponent implements OnInit {
numbers= [];
ngOnInit() {
function loadImages() {
this.numbers.push[1]
}
loadImages();
}
}
这似乎是
的上下文方面的问题。尝试将loadImages
功能置于ngonit
之外:
export class MyphotographsComponent implements OnInit {
numbers= [];
ngOnInit() {
this.loadImages();
}
private loadImages() {
this.numbers.push[1]
}
}
或将loadImages
写入箭头函数:
export class MyphotographsComponent implements OnInit {
numbers= [];
ngOnInit() {
const loadImages = () => this.numbers.push[1];
loadImages();
}
}