uniapp ordinary page jumps to tabBar page details (additional code + uni.switchTab use)

Hello everyone! In recent weeks, I have found that every Monday I will encounter a headache bug. Let me briefly introduce which headache problem I encountered today!

Problem Description

First of all, let me talk about the function to be realized:
I want to jump from a normal page to the page in the tabBar ( 这里指的页面是:小程序中底部导航那几个图标点击后所对应的页面). At first I used it uni.navigateTo, but 跳转没有效果I searched it on the Internet. If I want to use it, uni.switchTab来解决普通页面跳转到tabBar页面I tried it and there is no problem.
I have another question: For example A页面跳转到B页面,执行某些渲染B页面的方法, I was at the beginning B页面中的onLoad里面进行接收A页面的参数, but I found a problem!
If it is used onLoad,接收参数,并且每次跳转都要刷新页面,是不可以的, if it is used onShow可以解决这个问题, I specially sorted it out onShow和onLoad的区别?

The difference between onShow and onLoad

onLoad:It is triggered when the page is loaded, and is used for the initialization operation of the page, and it is only executed once.
onShow:It is triggered when the page is displayed, and is used to process the logic related to page display, which may be executed multiple times, including when the page is displayed for the first time and switched from the background to the foreground.
In my current scenario, it is more suitable to onShow
display in the form of the following code, and the normal page jumps to the tabBar page

solution

A page code
I made a value judgment here, and did not carry parameters. If you need to carry parameters directly index后面加个?跟参数名字和参数值, the example/pages/test/index?id=value

if(value==2){
    
    
 uni.setStorageSync('param1', value-2);
  uni.switchTab({
    
    
    url: '/pages/test/index',
  });
}

Page B code
I use the cache here, but I feel that there is no need to use the cache for this piece. I will remove it later and just pass parameters if your needs are the same as mine 每次跳转都要刷新页面,就放到onShow的里面.

onShow() {
    
    
   this.indexTab = uni.getStorageSync('param1');
   console.log('param1:', this.indexTab);
   uni.removeStorageSync('param1'); // 清除本地存储的参数,如果需要
},

conclusion

This time is 普通页面跳转到tabBar页面详解and onShow和onLoad的区别, I have been very busy on the project recently, and I can share some related ant design vuetechnologies later, including redis distributed cache, etc.
If you don't understand something, I will answer it for you for free, I hope it can help everyone!

Guess you like

Origin blog.csdn.net/xiaohua616/article/details/132287247