【发布时间】:2020-01-30 14:15:11
【问题描述】:
我有一个 while 循环,我试图在 VueJS 中设置一个数组。希望在数组中有 3 个对象,每个对象都是使用来自 4 个可能选择的 RNG 创建的。现在,当我遍历它们时,它所做的就是在每个数组槽中复制相同的对象:
methods: {
createMonster() {
const randomValue = Math.random();
while (this.i < 3) {
if (randomValue < 0.35) {
this.monster.push({
active: true,
type: 'Orc',
hp: 70
});
this.i++;
} else if (randomValue < 0.7) {
this.monster.push({
active: true,
type: 'Gremlin',
hp: 60
});
this.i++;
} else if (randomValue < 0.9) {
this.monster.push({
active: true,
type: 'Mage',
hp: 50
});
this.i++;
} else {
this.monster.push({
active: true,
type: 'Knight',
hp: 80
});
this.i++;
}
}
}
}
【问题讨论】:
-
将
randomValue的作业下移一行。
标签: javascript vue.js vuejs2