当前位置:网站首页>关于小程序tabbar不支持传参的处理办法
关于小程序tabbar不支持传参的处理办法
2022-06-26 08:38:00 【cms小程序插件【官方】】
无论是百度小程序还是微信小程序,app.json中规定的tabbar页面是不支持传参的,例如:
code腾石建站
- <navigator url='../service/service?typeid=6' openType="switchTab">
- 服务项目
- </navigator>
上面的navigater跳转有个属性:openType="switchTab"意思是打开tabbar页面service,虽然有个参数typeid=6,但是typeid=6并不能呗tabbar页面接收,造成打开tabbar页面的时候用于都是默认栏目。
(1)第一步:通过bindtap绑定方法触发缓存,把typeid存起来
虽然tabbar不支持传参,但是我们可以通过缓存的方式获取传递的参数,那么上面的navigator直接跳转的方式就不行了,需要封装一个方法:
- <view class='service_li' bindtap='show_service' data-typeid='11' data-listtype='3'>
- 服务项目
- </view>
index页面的“服务项目”通过bindtap绑定一个show_service方法,这个show_service会传递后边的data-*里边的所有参数。
在index.js中写上show_service方法:
- show_service: function (e) {
- var data = e.currentTarget.dataset;
- var title = data.title; //获取传递的title
- var typeid = data.typeid; //获取传递的typeid
- var listtype = data.listtype; //获取传递的listtype
- //通过setStorageSync方法将typed存入stypeid中,名字可以自己任意定
- wx.setStorageSync('stypeid', typeid)
- wx.switchTab({ //通过switchTab方法跳转到对应页面
- url: '/pages/service/service?typeid=' + typeid + "&title=" + title + "&listtype=" + listtype, //后边参数其实无效可以直接写成:‘/pages/service/service’
- })
- },
这样我们就把用户点击首页所传递的参数typeid存到缓存里边了
(2)第二步:在service中去获取typeid
在service.js中的onload或者onshow中加入typeid的方法,为了兼容用户是直接通过tabbar进入的情况,需要一个默认的typeid,参考代码:
- var stypeid = wx.getStorageSync('stypeid');//通过缓存获取typeid
- var typeid = stypeid ? stypeid : that.data.typeid; //如果stypeid存在读取,否则读取默认的typeid
然后我们可以直接通过getList()方法直接读取api获取内容列表了
- that.getList(typeid);
说明:
(1)如果你不是通过首页的show_service方法进入service页面的,那么就不会触发更新缓存,所以typeid的值是不会变的,通过点击tabbar进入service页面会永远显示某一个页面,你可以在onHide方法中增加一个重置或者情况stypied的方法,不过不建议这么弄
(2)service页面的切换栏目后,我们可以在switch_tab方法中加入修改缓存的方法,动态存入当前栏目的typeid
- switch_cat: function (e) {
- var that = this;
- var CATEGORYS = wx.getStorageSync('categorys')//调用栏目缓存
- var data = e.currentTarget.dataset;
- var typeid = data.typeid;
- var listtype = data.listtype;
- var curtypeid = data.typeid;
- that.setData({
- curtypeid: curtypeid,
- listtype: listtype,
- page: 1
- })
- wx.setNavigationBarTitle({
- title: CATEGORYS[curtypeid]['typename'] + '-' + wx.getStorageSync('system').seotitle
- });
- that.getList(typeid);
- },
不过,切换栏目时候,要不要更改缓存,根据自己需要,没必要必须切换
同理,如果你设置新闻页面、案例页面等页面的时候可以用同样的方法 存入缓存然后新闻、案例通过缓存判断需要显示的栏目
比如新闻:
- wx.setStorageSync('newsid', typeid)//存入newsid
边栏推荐
- How to correctly PIP install pyscipopt
- Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
- [unity mirror] use of networkteam
- Fourier transform of image
- Leetcode: array fast and slow pointer method
- Nebula diagram_ Object detection and measurement_ nanyangjx
- Ltp-- extract time, person and place
- 多台三菱PLC如何实现无线以太网高速通讯?
- 软件工程-个人作业-提问回顾与个人总结
- Detailed explanation of self attention & transformer
猜你喜欢

Mongodb分片环境搭建和验证(redis期末大作业)

【云原生 | Kubernetes篇】深入万物基础-容器(五)

直播回顾 | smardaten李鸿飞解读中国低/无代码行业研究报告:风向变了

Opencv learning notes II

Clion installation + MinGW configuration + opencv installation

Selenium 搭建 Cookies池 绕过验证反爬登录

三菱PLC若想实现以太网无线通讯,需要具备哪些条件?

【MATLAB GUI】 键盘回调中按键识别符查找表

WBC learning notes (I): manually push WBC formula

Opencv learning notes 3
随机推荐
Using MySQL and Qt5 to develop takeout management system (I): environment configuration
Performance comparison of unaryexpr's function on matrix elements in eigen Library
The principle and function of focus
Using transformers of hugging face to realize named entity recognition
Segmentation of structured light images using segmentation network
Stanford doggo source code study
基于SSM的电脑商城
Realizing sequence annotation with transformers
1.27 pytorch learning
Exploration of webots and ROS joint simulation (I): software installation
Drawing with MATLAB (1)
Regular Expression 正则表达式
Leetcode notes: binary search simple advanced
Yolov5进阶之一摄像头实时采集识别
Compiling owncloud client on win10
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
Whale conference provides digital upgrade scheme for the event site
The solution of positioning failure caused by framework jump
ROS learning notes (5) -- Exploration of customized messages
KNN resolution