在uniapp中,tabbar页面只能通过switchTab跳转,而switchTab跳转url不能带参数,我们希望带参数跳转回tabbar页面并且刷新该tabbar页面。
示例图,Tabbar页面:
B页面:
代码如下,在B页面中::
methods: {
// 跳转页面
goHome(cityid,city){
uni.setStorageSync('cityid', cityid);// 传参
uni.setStorageSync('city', city);
uni.$emit('refresh', { refresh: true });// 标记是否刷新tabbar页面
uni.switchTab({
url: '/pages/index/index' // 跳转回tabbar页面
});
},
},在tabbar页面中:
onShow: function() {
uni.$off('refresh'); // 建议先销毁一次监听,再进行新的一次监听,否则会出现重复监听的现象
uni.$once('refresh', (data) => {
// 刷新操作
if (data.refresh) {
this.city = uni.getStorageSync('city'); // 获取传参
this.newsList(); // 重载数据
}
});
},