1. 程式人生 > >微信小程式頁面跳轉傳值以及獲取值方法

微信小程式頁面跳轉傳值以及獲取值方法

在安卓中頁面跳轉傳值都是通過bundle,現在研究一下小程式的列表跳轉及頁面傳值。

my.wxml

<view class="container">

  <view bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  </view>

  <view class="info_list">
    <block wx:for="{{userListInfo}}" >
      <view class="weui_cell" data-index="{{item.index}}" id="{{item.index}}"
       bindtap="userinfo_item">
    
        <view class="weui_cell_hd">
          <image src="{{item.icon}}"></image>
        </view>
        <view class="weui_cell_bd">
          <view class="weui_cell_bd_p"> {{item.text}} </view>
        </view>
        <view wx:if="{{item.isunread}}" class="badge">{{item.unreadNum}}</view>
        <view class="with_arrow"></view>
      </view>
    </block>
  </view>

</view>
my.js

var app = getApp()
Page({
  data: {
    userInfo: {},
    userListInfo: [{
      icon: '../../images/iconfont-dingdan.png',
      text: '我的訂單',
      isunread: true,
      unreadNum: 2,
      index:1
    }, {
      icon: '../../images/iconfont-kefu.png',
      text: '聯絡客服',
      index: 5
    }, {
      icon: '../../images/iconfont-help.png',
      text: '常見問題',
      index: 6
    }]

  },

  onLoad: function () {
    var that = this
    //呼叫應用例項的方法獲取全域性資料
    app.getUserInfo(function (userInfo) {
      //更新資料
      that.setData({
        userInfo: userInfo
      })
    })
  },

  userinfo_item: function (e) {
    var index = e.target.dataset.index;
    console.log("----index----" + index)
     
    console.log('-----id-----'
      + e.currentTarget.id)
    var app = getApp();
    //設定全域性的請求訪問傳遞的引數 
    app.requestId = e.currentTarget.id;
    app.requestIndex = index;

  }

})


微信小程式設定id的方法標識來傳值

在要跳轉的item處,設定一個id並給當前的id賦值上對應的key值,

如 id="{{item.index}}"

後我們在js的bindtap的響應事件中獲取,並傳遞到下一個介面中;
獲取到id傳的值

通過e.currentTarget.id;獲取設定的id值,並通過設定全域性物件的方式來傳遞數值,
獲取全域性物件 var app=getApp(); //設定全域性的請求訪問傳遞的引數 app.requestDetailid=id;

在除錯模式下:我們也可以在,wxml中檢視到我們設定的每一個item的id值

通過使用data - xxxx 的方法標識來傳值


通過使用data - xxxx 的方法標識來傳值,xxxx可以自定義取名 比my.wxml中的data-index。

如何獲取data-xxxx傳遞的值?
在js的bindtap的響應事件中:
通過資料解析一層層找到資料,var id=e.target.dataset.index(根據你的data-id的取名)

如js中的兩個列印就是通過兩種不同方式獲得的id。

微信小程式如何跨頁面獲取值?
依據上面的方式設定要傳遞的值,頁面跳轉後,我們就需要在下一個頁面拿到傳遞的資料(這個資料在傳遞前,就已經被設定成全域性變數)相當於給全域性變數添加了新的key,value
在跳轉後的js頁面,接收傳遞過來的資料detail.js
同樣通過全域性額方式取值出來,(即和app.js中取某個變數的值是一樣的)
var id=getApp().requestId;

var index=getApp().requestIndex;

console.log(id);

console.log(index);

通過連結傳參:
      wx.navigateTo({
        url: '/pages/account/feedback/feedback?test=feedback_test&name=jia',
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })



點選頁面跳轉時通過?方式傳參。在跳轉後的頁面JS中做如下接收:
onLoad: function (e) {
    var movieid = getApp().requestId;
    var movieIndex = getApp().requestIndex;
    console.log("-----feedback--movieid--" + movieid +" " + movieIndex);
    console.log("-----feedback--test--" + e.test);
    console.log("-----feedback--name--" + e.name);
  },

感覺比較好的方法還是通過連結方式進行引數傳遞,第一種有些像安卓中進行頁面跳轉,把一些傳遞的引數寫到Application中,第二種是像通過bundle方式進行傳遞。前端小白總結,希望前端豐富的同學可以提供更多思路。