Angular 财产';结果';不存在于类型';void'?
我在.ts中有此方法:Angular 财产';结果';不存在于类型';void'?,angular,Angular,我在.ts中有此方法: search(event) { this.autocompletedata.forEach(function(entry) { this.result.push(entry['items'].filter(a=>a['item'])); }); console.log(this.result,'result'); } 但是我不能使用这个结果,但是我不知道为什么。当我声明时,让result我可以使用,但是这个.
search(event) {
this.autocompletedata.forEach(function(entry) {
this.result.push(entry['items'].filter(a=>a['item']));
});
console.log(this.result,'result');
}
但是我不能使用
这个结果,但是我不知道为什么。当我声明时,让result
我可以使用,但是这个.result我得到了这个错误。有什么建议吗?不知何故,这个.result
在方法内部是不可见的,或者什么?问题是您在forEach内部创建了一个privat函数。此函数的作用域不同。如果使用箭头函数,则作用域保持不变,并且您可以使用this.result
search(event) {
this.autocompletedata.forEach((entry) => {
this.result.push(entry['items'].filter(a=>a['item']));
});
console.log(this.result,'result');
}
问题是您在forEach中创建了一个privat函数。此函数的作用域不同。如果使用箭头函数,则作用域保持不变,并且您可以使用this.result
search(event) {
this.autocompletedata.forEach((entry) => {
this.result.push(entry['items'].filter(a=>a['item']));
});
console.log(this.result,'result');
}
this.
在使用function(entry){
如果你把它改成
(entry) => {
你会得到想要的行为
另请参见此。在使用函数(条目){
如果你把它改成
(entry) => {
你会得到想要的行为
另请参见就这样..tnx我会接受你的答案,因为你是第一名:)就这样..tnx我会接受你的答案,因为你是第一名:)