微信小程序 頁面跳轉傳參詳解

微信小程序 頁面跳轉傳參,做微信小程序必定會用的這樣的功能,這裏就記錄下本人學習實現代碼資料。

剛接觸微信小程序,多裏面的語法和屬性還不怎麼聊解,如有不多的地方希望各位大神多多指教。今天來說下微信小程序怎麼跳轉和傳參,話不多說直接上代碼。

實現的功能是給列表增加點擊功能傳參到下一頁;

   

代碼如下:

<import src="../WXtemplate/headerTemplate.wxml"/> 
<view> 
 <!--滾動圖--> 
 <view> 
 <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoPlay}}" interval="{{intervalTime}}" duration="{{Time}}"> 
 <block wx:for="{{imageURl}}"> 
  <swiper-item> 
   <image src="{{item}}" class="imagePX"></image> 
  </swiper-item> 
 </block> 
 </swiper> 
 </view> 
 <!--功能按鈕--> 
 <view class="section-bg"> 
 <block wx:for="{{buttonNum}}"> 
  <!--模版--> 
  <template is="buttonList" data="{{item}}"/> 
  <!--<view class="section-item"> 
  <image class="section-img" src="{{item.image}}"></image> 
  <text class="section-text">{{item.text}}</text> 
 </view>--> 
 </block> 
 </view> 
 <!--資訊列表--> 
 <view> 
 <block wx:for="{{listNum}}"> 
  <template is="newList" data="{{item,index}}"/> 
 </block> 
  
 </view> 
 
 
 </view> 

其中

<template is="buttonList" data="{{item}}"/> 

爲模版代碼如下

<template name="buttonList"> 
 <view class="section-item"> 
  <image class="section-img" src="{{item.image}}" bindtap="buttonClick"></image> 
  <text class="section-text">{{item.text}}</text> 
 </view> 
</template> 
 
<!--list--> 
<template name="newList"> 
 <view class="section-list" bindtap="listClick" id="{{index}}"> 
 <view> 
  <image class="list-img" src="{{item.image}}"></image> 
 </view> 
 
  <view class="section-textt"> 
  <view class="title"><text>{{item.title}}</text></view> 
  <view class="subTitle"><text>{{item.subTitle}}</text></view> 
 </view> 
 </view> 
 
</template> 

這裏只爲下面的列表增加了點擊方法

點擊列表js代碼

listClick:function(event){ 
 console.log(event); 
 var p = event.currentTarget.id 
 
 wx.navigateTo({url:'/pages/xiangqing/xiangqing?id=上一頁的參數'}) 
 } 

其中

wx.navigateTo({url:'/pages/xiangqing/xiangqing?id=上一頁的參數'}) 

爲跳轉方法,id爲需要傳的參數 如果參數爲動態參數代碼如下: 

listClick:function(event){ 
 console.log(event); 
 var p = event.currentTarget.id 
 
 wx.navigateTo({url:'/pages/xiangqing/xiangqing?id='+p}) 
 } 

其中p爲上面爲每一行設置的id值

在下一頁取值代碼如下: 

 data:{ 
 // text:"這是一個頁面" 
 title:'' 
 }, 
 onLoad:function(options){ 
 // 頁面初始化 options爲頁面跳轉所帶來的參數 
 this.setData({ 
 title:options.id 
 }) 

然後在頁面上顯示代碼如下:

<view>{{title}}</view> 

最終實現效果:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章