将数组推送到 Vue3 打字稿中相同类型的数组

Push an array to an array of the same type in Vue3 typescript

我有一个 Obj1 数组。在 vue 3+ts 项目中,单击“加载更多”按钮后,我通过 axios 调用后端服务并检索相同类型的数组。我想将这些行附加到前一个数组,但它不起作用:

这个.Array1.push(response.data)

如果我当时添加 1 个项目,Array1 会更新:

这个.Array1.push(response.data[0])

我错过了什么?

根据您的问题,此代码有效:

this.Array1.push(response.data.results[0])

表示response.data.results是你要处理的数组。如果你想把整个数组推进去,你可以简单地使用 ES6 spread operator:

this.Array1.push(...response.data.results)

你在推动不同的价值观。 response.data.results是一个数组,而response.data是一个json数组的数据。

所以,您可能只想这样做:

this.Array1 = response.data.results