我已经研究了 Vue.js
几个星期,并使用 NodeJS Express
开发了一个 Rest API 后端,我想连接到它。我已经让 Rest API 正常工作并为我提供 JSON。现在,我尝试获取该 json
并将其存储在我的 Vue 组件之一中的数组中,使用 vue-resource
{
"status": 200,
"error": null,
"response": [
{
"TxnID": "277731-1507125498",
"SalesRep": null,
"QuoteNumber": "20822PF",
"QuoteModified": "2017-10-23T17:46:25.000Z",
"Customer": "Dquard",
"Country": "Indonesia",
"QuoteAmount": 9106,
"SalesOrderNumber": "19289",
"SalesOrderAmount": 9106,
"IsFullyInvoiced": "true",
"status": "WIN 100%",
"type": null,
"margin": null
},
{
//more data
}
]
}
我想将每个响应
放入一个数组对象中。我正在尝试用一种方法来做到这一点:
methods: {
loadData () {
this.$http.get(this.url)
.then(response => {
return response.json()
}).then(data => {
const resultArray = []
for (let key in data.response) {
resultArray.push(data[key])
}
console.log(resultArray)
// this.salesOrders = resultArray
})
}
但是当控制台显示 resultArray
时,它只是一堆 未定义
:
我需要更改 Rest API 提供 json 的方式吗?理想情况下,我希望 json 中的每个响应对象将每个记录作为键/值对提供。例如 SalesRep: 'Jason'
而不是像 'SalesRep':'Jason'
最佳答案
问题出在您的 for
循环中,您没有访问响应数组。
for (let key in data.response) {
resultArray.push(data.response[key])
}
关于node.js - 将数据从我的其余 api 提取到我的 VueJS 组件的数组数据属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52521732/