After uni-app solves the calling method of page A, it automatically triggers the method of page B

Project scenario:

The tabbarA page triggers a click event, which requires the tabbarB page to automatically trigger the request interface method


Problem Description

After the tabbar A page triggers a click event, the tabbarB page requests the interface in the onload hook function. Only when the tabbarB page is opened for the first time, the onload hook function request interface will be triggered, and the onload hook function will not be triggered when the tabbarB page is jumped again, and the onload hook function will not be triggered in the onshow. too often

solution:

Tabbar A page

//在点击事件后
uni.$emit('orderUpdate', {
    
    msg:'A页面点击了'})

Tabbar B page

onload(){
    
    
	//首页接单后 更新order 请求接口
	uni.$on('orderUpdate', (data) => {
    
    
	console.log('tabbar B页面需更新',data)
	//调用请求接口方法
	....
}
//注意!!! 
//一定要在页面卸载时 卸载监听事件  
//否则会造成多次触发监听的情况
onUnload() {
    
    
	//卸载监听事件
	uni.$off('orderUpdate');
},

Guess you like

Origin blog.csdn.net/Gik99/article/details/131794444