Vuejs,如何构建对象? [英] Vuejs, how to build an object?
问题描述
您好,我试图通过API在我的网站上显示图形,以在列中显示图形,我需要向他发送一个像这样的对象
Hello I'm trying to display graphics on my website from API, to display my graphic in column, I need to send him an object like this one
data: [{
name: 'Data1',
data: [
['ALIONIVS', 1],
['GARCIC(I)VS', 1],
['NONIVS', 1],
['SERVERS,A', 1]
],
},
{
name: 'Data2',
data: [
['TVTOR', 1],
['FVSCINVS', 1],
['GVTAMVS / GVMATIVS', 1],
['SEVERINVS, -A', 1],
['TVSCVS, -A / TVSGVS', 1],
['VEIENTA', 1],
]
}
],
但是我想拥有动态数据 我正在使用axios从我的api检索数据.
But I would like to have dynamic data I'm retrieving data from my api with axios.
axios
.get('../api/civitasnomen/' + this.searchInputcivitas)
.then(response => {
this.data1 = response.data.map(item => {
return [item.lemme, item.nb];
})
})
axios
.get('../api/civitascognomen/' + this.searchInputcivitas)
.then(response => {
this.data2 = response.data.map(item => {
return [item.lemme, item.nb];
})
})
在我的对象中,我想同时检索我的api数据
In my object I would like to retrieve both my api data
如果在axios中编写此代码:
If write this in axios :
axios .get('../api/civitasnomen/'+ this.searchInputcivitas) .then(response => this.data1 = response.data)
axios .get('../api/civitasnomen/' + this.searchInputcivitas) .then(response => this.data1 = response.data )
例如Data1 =
[ { "nb": 1, "lemme": "ALIONIVS" }, { "nb": 1, "lemme": "GARIC(I)VS" }, { "nb": 1, "lemme": "NONIVS, -A" }, { "nb": 1, "lemme": "SEVERVS, -A" } ]
和Data2 =
[ { "nb": 1, "lemme": "TVTOR" }, { "nb": 1, "lemme": "FVSCINVS" }, { "nb": 1, "lemme": "GVTAMVS / GVMATIVS" }, { "nb": 1, "lemme": "SEVERINVS, -A" }, { "nb": 1, "lemme": "TVSCVS, -A / TVSGVS" }, { "nb": 1, "lemme": "VEIENTA" } ]
推荐答案
假定您已将data
属性初始化为空数组:
Suppose that you have data
property initialized as an empty array:
data(){
return {
data: [],
}
}
然后使用来自API的结果对其进行更新:
Then update it with result coming from API :
axios
.get('../api/civitasnomen/' + this.searchInputcivitas)
.then(response => {
this.data.push({
name:'data1',
data:response.data.map(item => {
return [item.lemme, item.nb];
})
})
})
axios
.get('../api/civitascognomen/' + this.searchInputcivitas)
.then(response => {
this.data.push({
name:'data2',
data:response.data.map(item => {
return [item.lemme, item.nb];
})
})
这篇关于Vuejs,如何构建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!