bug:wx.switchTab: url 不支持 queryString

uni-app 开发小程序时 ,bug:wx.switchTab: url 不支持 queryString

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

image

在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;
    },
}

猜你喜欢

转载自blog.csdn.net/YZRHANYU/article/details/131107048