小程序 · 2024年7月10日

微信小程序页面跳转传参详解

这篇文章主要介绍了微信小程序 页面跳转传参详解的相关资料,并附简单实例,和实现效果图,需要的朋友可以参考下

 微信小程序 页面跳转传参,做微信小程序必定会用的这样的功能,这里就记录下本人学习实现代码资料。

刚接触微信小程序,多里面的语法和属性还不怎么聊解,如有不多的地方希望各位大神多多指教。今天来说下微信小程序怎么跳转和传参,话不多说直接上代码。

实现的功能是给列表增加点击功能传参到下一页;

   

代码如下:

<import></import> 
<view> 
 <!--滚动图--> 
 <view> 
 <swiper> 
 <block> 
  <swiper-item> 
   <image></image> 
  </swiper-item> 
 </block> 
 </swiper> 
 </view> 
 <!--功能按钮--> 
 <view> 
 <block> 
  <!--模版--> 
  <template></template> 
  <!--<view class="section-item"> 
  <image class="section-img" src="{{item.image}}"></image> 
  <text class="section-text">{{item.text}}</text> 
 </view>--> 
 </block> 
 </view> 
 <!--资讯列表--> 
 <view> 
 <block> 
  <template></template> 
 </block> 
  
 </view> 
 
 
 </view>

登录后复制

其中

 

为模版代码如下

<template> 
 <view> 
  <image></image> 
  <text>{{item.text}}</text> 
 </view> 
</template> 
 
<!--list--> 
<template> 
 <view> 
 <view> 
  <image></image> 
 </view> 
 
  <view> 
  <view><text>{{item.title}}</text></view> 
  <view><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 
 })

登录后复制

然后在页面上显示代码如下:

{{title}}  

最终实现效果:

以上就是微信小程序页面跳转传参详解的详细内容,更多请关注GTHOST其它相关文章!