1. 程式人生 > >微信小程式資料傳值與引用data變數

微信小程式資料傳值與引用data變數

1、在微信小程式中通過點選方法進行變數賦值,可以將數值定義在e.currentTarget.dataset裡

通過定義data-x,將index值進行傳遞

<block wx:for="{{imgUrls}}" wx:key="{{index}}">
    <swiper-item>
       <image src='{{imgUrls[index].imgSrc}}' class='slide-image' bindtap='func_swiper' data-x="{{index}}"></image>
     </swiper-item
>
</block>

2、在點選方法func_swiper進行資料取值

// 點選banner跳轉
  func_swiper: function(e){
    var index = e.currentTarget.dataset.x;
    console.log(index);
   }

此處在方法中取出e.currentTarget.datase.x值即為該index值

3、在js中data內定義了typeIndex變數,在之後的js 方法中引用需要以下使用:

Page({

  /**
   * 頁面的初始資料
   */
  data: {
      typeIndex: 0
} }]

使用that代替this,進行取值

 var that = this;
 console.log(that.data.typeIndex);