微信小程序页面传递循环列表数据

2025-10-27 23:30:44

1、打开开发者工具,在项目pages文件夹下,新建目录,在目录内新建mypage,并在app.json中将mypage设为第一页面

微信小程序页面传递循环列表数据

2、在mypage.wxml中写代码如下:

<view wx:for="{{datary}}" data-index="{{index}}" data-cont="{{item}}" class="item {{curItem==index?'focus':''}}" catchtap="itemtap">

{{index}}-{{item}}

</view>

微信小程序页面传递循环列表数据

3、在mypage.wxss中写代码如下:

.item{

 width: 100rpx;

 height: 100rpx;

 margin: 20rpx;

 background-color: rebeccapurple

}

.focus{

 background-color: red;

}

微信小程序页面传递循环列表数据

4、在mypage.js中添加数据如下:

 data: {

   datary:['hello','world'],

   curItem:0

 },

微信小程序页面传递循环列表数据

5、在mypage.js中为点击事件添加响应函数,代码如下:

 itemtap:function(e){

   console.log(e)

   var that = this;

   that.setData({curItem:e.target.dataset.index})

   wx.navigateTo({

     url: '../logs/logs?item='+e.target.dataset.cont,

   })

 }

微信小程序页面传递循环列表数据

6、在logs.wxml中添加代码如下:

<view>{{datavar}}</view>

微信小程序页面传递循环列表数据

7、在logs.js中添加数据接收传递过来的参数,在onload事件中显示数据,代码如下:

Page({

 data: {

   logs: [],

   datavar:null,

 },

 onLoad: function (options) {

   console.log(options)

   this.setData({

     

     datavar:options.item

   

   })

 }

})

微信小程序页面传递循环列表数据

8、点击编译,运行代码,在左侧模拟器,点击列表项,会跳转到logs页面,并成功传递了参数

微信小程序页面传递循环列表数据

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢