微信小程序item頁面傳參的案例:
A頁面
data-id="{{item.id}}"為標記列表的下標,
item.id的來源與wx:for="{{ components }}"的列表渲染
bindtap="readArticle"點擊事件
在for循環的時候會報異常,加wx:key=""異常就不會出現了
wxml文件代碼
<view class="grids-small"><block wx:for="{{ components }}" wx:key="">
<view class="grid" bindtap="readArticle" data-id="{{item.id}}">
<view class="grid_icon">
<image class="grid_icon" src="../resources/room.jpg" alt="" />
</view>
<view class="grid_label">{{ item.title }}</view>
</view>
</block>
</view>
js文件代碼
readArticle:function(e){var $data = e.currentTarget.dataset;
console.log('$data===啊哈哈' + $data.id);
wx.navigateTo({
url: '/pages/index/index?id='+ $data.id ,
})
}
B頁面代碼
/*** 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
var $data = options.id;
console.log('onLoad' + $data)
console.log('options======' + $data);
},