欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)傳值以及獲取值的方法分析

 更新時間:2017年12月18日 10:28:36   作者:jia635  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)傳值以及獲取值的方法,結(jié)合實例形式總結(jié)分析了微信小程序頁面跳轉(zhuǎn)及傳值的常用操作技巧,需要的朋友可以參考下

本文實例講述了微信小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)傳值以及獲取值的方法。分享給大家供大家參考,具體如下:

在安卓中頁面跳轉(zhuǎn)傳值都是通過bundle,現(xiàn)在研究一下小程序的列表跳轉(zhuǎn)及頁面?zhèn)髦怠?/p>

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: '聯(lián)系客服',
   index: 5
  }, {
   icon: '../../images/iconfont-help.png',
   text: '常見問題',
   index: 6
  }]
 },
 onLoad: function () {
  var that = this
  //調(diào)用應用實例的方法獲取全局數(shù)據(jù)
  app.getUserInfo(function (userInfo) {
   //更新數(shù)據(jù)
   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();
  //設(shè)置全局的請求訪問傳遞的參數(shù)
  app.requestId = e.currentTarget.id;
  app.requestIndex = index;
 }
})

微信小程序設(shè)置id的方法標識來傳值

在要跳轉(zhuǎn)的item處,設(shè)置一個id并給當前的id賦值上對應的key值,
id="{{item.index}}"
后我們在js的bindtap的響應事件中獲取,并傳遞到下一個界面中;
獲取到id傳的值
通過e.currentTarget.id;獲取設(shè)置的id值,并通過設(shè)置全局對象的方式來傳遞數(shù)值,
獲取全局對象 var app=getApp(); //設(shè)置全局的請求訪問傳遞的參數(shù) app.requestDetailid=id;
在調(diào)試模式下:我們也可以在,wxml中查看到我們設(shè)置的每一個item的id值

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

通過使用data - xxxx 的方法標識來傳值,xxxx可以自定義取名 比my.wxml中的data-index。
如何獲取data-xxxx傳遞的值?
在js的bindtap的響應事件中:
通過數(shù)據(jù)解析一層層找到數(shù)據(jù),var id=e.target.dataset.index(根據(jù)你的data-id的取名)
如js中的兩個打印就是通過兩種不同方式獲得的id。

微信小程序如何跨頁面獲取值

依據(jù)上面的方式設(shè)置要傳遞的值,頁面跳轉(zhuǎn)后,我們就需要在下一個頁面拿到傳遞的數(shù)據(jù)(這個數(shù)據(jù)在傳遞前,就已經(jīng)被設(shè)置成全局變量)相當于給全局變量添加了新的key,value
在跳轉(zhuǎn)后的js頁面,接收傳遞過來的數(shù)據(jù)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) {},
})

點擊頁面跳轉(zhuǎn)時通過?方式傳參。在跳轉(zhuǎn)后的頁面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);
 },

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

希望本文所述對大家微信小程序開發(fā)有所幫助。

相關(guān)文章

最新評論