0
我喜歡用listenSwiper來捕捉swiper模塊中的滑動,改變當前項目樣式的前一項和下一項。如何在WeChat Mini程序的setData函數中使用變量作爲索引?
listenSwiper: function(e) {
var prev = 'items['+(e.detail.current-1)+'].x'
var now = 'items['+(e.detail.current)+'].x'
var next = 'items['+(e.detail.current+1)+'].x'
console.log(prev)
console.log(now)
console.log(next)
this.setData({
prev:12,
now:0,
next:-12,
})
},
問題是代替識別prev
,now
和next
作爲變量,使用setData處理它們作爲串,這意味着它在現有陣列中增加了三個新鍵,而不是修改數組作爲這樣:
this.setData({
'items[0].x':12,
'items[1].x':0,
'items[2].x':-12,
})