我试图动态更改变量,以免使用太多的switch语句。我希望这样做:
this.variable1 = 2
this.variable2 = 3
var array1 = [this.variable1, this.variable2]
然后再做
array1[0] = 25
array1[1] = 12
console.log(array1[0]) //would output 25
console.log(array1[1]) // would output 12
当然,这不是正常情况,但我怎样才能动态实现呢?必须有一种方式我确定。
答案 0 :(得分:0)
我认为这正是您所寻找的:http://jsfiddle.net/yMv7y/3067/
var array1
var demo = new Vue({
el: '#demo',
data: {
variable1: 2,
variable2: 3
},
mounted() {
array1 = [this.variable1, this.variable2]
},
methods: {
test() {
array1[0] = 25
array1[1] = 12
console.log(array1[0]) //would output 25
console.log(array1[1]) // would output 12
}
}
})