2017-04-08 39 views
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, 
    }) 
    }, 

問題是代替識別prevnownext作爲變量,使用setData處理它們作爲串,這意味着它在現有陣列中增加了三個新鍵,而不是修改數組作爲這樣:

this.setData({ 
    'items[0].x':12, 
    'items[1].x':0, 
    'items[2].x':-12, 
}) 

回答

相關問題