##1.函数传参:
<!--bindtap--事件名;personalCollect--函数名; data-type---参数 -->
<text class="flex {{collectType==0?'collectActive':''}}" bindtap="personalCollect" data-type="0">旅行</text>
<text class="flex {{collectType==1?'collectActive':''}}" bindtap="personalCollect" data-type="1">摄影</text>
js :获取参数值
//js文件
personalCollect (event) {
let num = event.currentTarget.dataset.type//获取event传过来的参数
},
##2.页面跳转(跳转至新页面)
<!-- wxml文件-->
<!--bindtap绑定函数,点击跳转 -->
<view bindtap="onSkip">跳转页面</view>
//js文件
onSkip () {
//wx.navigateTo:保留当前页面,跳转到应用内的某个页面
//url:要跳转的页面路径
wx.navigateTo({
url: '../pages/sonPage/collect',
})
},
传多个参数跳转:
<!-- 父页面-->
<view bindtap="onSkip" data-url="./sonPage/details?id={{article.id}}" data-city="{{article.cityName}}">
onSkip(e) {
let url = e.currentTarget.dataset.url
let city = e.currentTarget.dataset.city
wx.navigateTo({
`${url}&city=${city}` //每个值用&分隔, city是为了子页面更好获取值
})
},
//子页面获取父页面的参数
onLoad:(options)=>{
console.log(options)//这里面包含传的值
}
总结:
函数传参:由于微信小程序开发不同于以往的普通web开发, 无法通过js获取wxml文件的dom结构,但可以通过微信小程序数据绑定以及view标签的”data-xx“自定义属性去更改标签类名,传值通过wxml设置data-[参数名]传递参数,[参数名]只能是小写,不能有大写
页面跳转:页面跳转还有种跳转叫 wx.switchTab,它是跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面,用法都差不多,可以看看官方文档
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.switchTab.html