应用场景:页面A跳转页面B,在页面B操作完成将结果返回页面A,返回是通过uni.navigateBack()方式返回,这个时候页面A是不会自动刷新页面的,但是页面B要将结果返回到页面A,在这种情景下就需要用到全局自定义事件来进行页面通讯。
具体看代码实现:
首先页面A创建一个自定义的监听事件 refreshPage,通过回调函数参数来接收页面B返回的结果。
onLoad((options)=>{
	uni.$on("refreshPage",(arg)=>{
		getAddressData(arg);
	})
})页面B:我这里的业务是这样的。下面是一个用户的地址列表,用户选择完地址之后自动返回页面A,并将用户选择的地址信息在页面A中展示出来。
<radio-group @change="radioChange">
	<view class="row"   v-for="item in addressList" :key="item.id">
		<view class="row-1"><radio :value="item.id" :checked="index === current"  /></view>
		<view class="row-2">
			<view class="row-title">{{item.username}} {{item.phone}}</view>
			<view class="row-address">{{item.province}}{{item.city}}{{item.area}}{{item.detail}}</view>
		</view>
		<view class="row-3">
			<image src="../../static/icons/edit.png" class="imgicon" />
			<!-- <image src="../../static/logo.png" class="imgicon" style="margin-left:20rpx" /> -->
		</view>
	</view>
</radio-group>const radioChange = (e)=>{
	console.log(e)
	let id = e.detail.value;
	uni.navigateBack({
		success: () => {
			uni.$emit('refreshPage', {"addressid":id})
		}
	})
}通过uni.$emit 创建一个自定义事件 refreshPage ,同时传递参数。
最后在页面A退出的时候需要将全局的自定义事件refreshPage移除,需要在页面A中添加onUnload()事件。
onUnload(()=>{
    uni.off("refreshPage")
})这样就可以实现页面之间的通讯。这是组合式API的写法。
 
       
         
        
 
                
                 
                
                 
                
                 
                
                