在首页分类入口跳转到具体某一分类时,分类页是项目的底导页面,存在于tabbar里,需要使用switchTab进行跳转。参数是分类的id,用来展示分类的某一具体分类。
此时报错wx.switchTab: url 不支持 queryString

在uni-app和微信小程序的API中明确表示,路径后不能带参数。 方案一: 思路就是通过storage 来缓存数据, 跳转到指定页后获取storage , 并清除数据。 需要特别注意的是, 跳转到tabBar页面,并非关闭其他所有非tabBar的页面,跳转发生时 可能指定页面已经onLoad 的情况下, 所以可以把获取参数的逻辑写在onShow 里。 跳转代码: uni.setStorageSync('categoryId','254')
uni.switchTab({
url:'/pages/tabbar/category'
})
获取参数的代码: onShow(){
try {
const result = uni.getStorageSync('categoryId')
if (result) {
uni.removeStorage({
key: 'categoryId'
})
}
} catch (error) {
}
},
方案二: uni-app开发的可以使用vuex 来存储categoryId,但是一定要记得用完清掉。接收时可以把获取参数的逻辑写在computed 里。 store下index.js的代码定义以初始化的方法: state:{
categoryId:''
},
mutations: {
setCategoryId(state,data){
state.categoryId = data;
},
需要赋值跳转的代码: this.$store.commit('setCategoryId',254)
uni.switchTab({
url:'/pages/tabbar/category'
})
需要获取的代码: computed: {
categoryId(){
return this.$store.state.categoryId;
},
} |