问题:在使用uniapp开发小程序应用中使用了 wx.navigateTo 结合 open-type="switchTab" 跳转到 tabBar 页面之后 就无法把上个页面的参数传递过来。
提供 解决跳转到 tabBar 页面传递参数 的两种方法
1.使用 wx.reLaunch() 可以进行传参
<view open-type="switchTab" @click="addCart">
加入购物车
</view>
<script>
export default {
data() {
return {
goods_id: null, //本商品id
};
},
methods:{
addCart(){
wx.reLaunch({
url:"/pages/cart/index?goods_id="+ this.goods_id
})
}
}
}
</script>
2 本地数据存储 uni.setStorage()
在跳转页面之前进行本地数据存储 uni.setStorage()
<script>
export default {
data() {
return {
goods_id: null, //本商品id
};
},
methods:{
addCart(){
//数据持久化 key:存储指定的键,data:需要存储的数据
uni.setStorage({
key: "goods_id",
data: this.goods_id,
success() {
console.log('成功了');
},
fail() {
console.log('缓存失败了');
}
})
wx.navigateTo({
url:"/pages/cart/index"
})
}
}
}
</script>
到下一个页面获取数据
onLoad(options) {
try {
const res = uni.getStorageSync("goods_id")
console.log(res) //取到数据参数
} catch (e) {}
},