【发布时间】:2021-03-23 09:24:24
【问题描述】:
我对 Vue 非常陌生,对于一个项目,我正在尝试根据数字创建一组对象。例如,如果总长度值为 3,那么有没有办法创建 fetchList1、fetchList2 和 fetchList3?如果总长度值为 2,则应将数据返回对象创建为 fetchList1 和 fetchList2。
我从数据库中获取总长度值,因此它有时可能大于 50 或小于 5。
查看
<div id="app">
<button @click="grabTeams()">
CLICK ME
</button>
</div>
方法
new Vue({
el: "#app",
data: {
totalLength: '3',
fetchList1: '',
/** if the total length is 3 then it should automatically create fetchList1, fetchList2 and fetchList3 **/
},
methods: {
toggle: function(todo){
todo.done = !todo.done
},
grabTeams(){
console.log('Total value length ' +this.totalLength);
for(let b=0; b < this.totalLength; b++){
console.log('value of '+b);
var replyDataObj1 = parseInt(b);
replyDataObj1={
"id" : b
}
this['fetchList'+b] = replyDataObj1;
}
},
}
})
下面是我在 jsfiddle 上试过的链接
【问题讨论】:
标签: javascript arrays vue.js vuejs2 vue-component