gpt4 book ai didi

javascript - 在 foreach 方法中更新数组 Angular

转载 作者:行者123 更新时间:2023-11-28 17:57:01 25 4
gpt4 key购买 nike

大家好,遇到了一个错误,我认为该错误与我希望能得到一些建议的范围有关。我有一个正在循环的数组,我想将结果推送到其中。 当我尝试将结果推送到 this.test.forEach 中的 public myArray:Array 时,我收到以下错误:void 类型上不存在。非常感谢任何建议。

 export class componentName implements OnInit {
public myArray:Array<any>;

ngOnInit() {
this.test = diff(this.example1, this.example2);

this.test.forEach(function(part){
let span = document.createElement('span');
span.appendChild(document.createTextNode(part.value));

this.myArray.push(span); // error does not exist on type void
});
}

最佳答案

您的第一个错误是您使用了错误的this作用域。

this.test.forEach(function(part){

应该是

 this.test.forEach((part)=>{
<小时/>

并且您需要初始化数组。

public myArray:Array<any> = [];

关于javascript - 在 foreach 方法中更新数组 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257295/

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