如何将数组推入打印机中的另一个非空数组?参加如果需要
问题描述:
这个数组包含ATLEAST 10个对象如何将数组推入打印机中的另一个非空数组?参加如果需要
array: any[] = [];
调用函数时,阵列应该包含现有的和新的项目对象。
function() {
var timeline = this.service.fetchservice(10)
.map((result : Response) => result.json())
.subscribe(item=> {
this.array.push(item);
});
}
在控制台错误
ERROR TypeError: _this.array.push is not a function
答
类似:
this.results = this.results.concat(data.results);
答
当使用function()
,这需要在功能上本身的上下文。你应该使用Typescript类的方法,例如:
export class TestApp {
someMethod(): void {
// does something
}
}
+0
:void没有提出任何错误,但无法分辨好处。你能解释一下吗? – SamYah
试过但得到这个错误。 _this.results.concat不是函数 – SamYah
那么你没有定义你的this.array – Carsten
Concat的工作。谢谢卡斯滕。 我的问题是,覆盖this.array,而不是[]。我试过.push,现在它也工作了。 – SamYah