当前位置: 代码网 > it编程>编程语言>Javascript > uni-app实现页面通信EventChannel的操作方法

uni-app实现页面通信EventChannel的操作方法

2024年06月11日 Javascript 我要评论
uni-app实现页面通信eventchannel之前使用了eventbus的方法实现不同页面组件之间的一个通信,在uni-app中,我们也可以使用uni-app api ——

uni-app实现页面通信eventchannel

之前使用了eventbus的方法实现不同页面组件之间的一个通信,在uni-app中,我们也可以使用uni-app api —— uni.navigateto来实现页面间的通信。注:2.8.9+ 支持页面间事件通信通道。

1. 向被打开页面传送数据

// index.vue
<script setup>
	uni.navigateto({
		url: '/pages/tender/detail', // 跳转详情页面
	    success:function(res){
	      // 通过eventchannel向被打开页面传送数据
	      res.eventchannel.emit('todetailemits', { data: 'index to detail' })
	    }
	});
</script>
// detail.vue
import { onload } from '@dcloudio/uni-app';
import { ref, getcurrentinstance} from 'vue';
const instance = getcurrentinstance().proxy
<script setup>
	onload(()=>{
		const eventchannel = instance.getopenereventchannel();
		eventchannel.on('todetailemits',(data)=>{
		  console.log(data,'data') // 输出结果如下
		})
	})
</script>

2. 如果需要获取被打开页面传送到当前页面的数据

// index.vue
<script setup>
	uni.navigateto({
		url: '/pages/tender/detail', // 跳转详情页面
	    events:{
	      // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
	      updataemits:function(data){
	        console.log(data,'data index')  // 输出结果如下
	        // 可以在当前页做一些操作....
	      }
	    },
	    success:function(res){
	      // 通过eventchannel向被打开页面传送数据
	      res.eventchannel.emit('todetailemits', { data: 'index to detail' })
	    }
	});
</script>
// detail.vue
import { onload } from '@dcloudio/uni-app';
import { ref, getcurrentinstance} from 'vue';
const instance = getcurrentinstance().proxy
<script setup>
	// 如点击某一按钮
	const cancle = () => {
		const eventchannel = instance.getopenereventchannel();
	    eventchannel.emit('updataemits',{data:'detail to index'})
	    uni.navigateback()
	}
	onload(()=>{
		const eventchannel = instance.getopenereventchannel();
		eventchannel.on('todetailemits',(data)=>{
		  console.log(data,'data') 
		})
	})
</script>

到此这篇关于uni-app实现页面通信eventchannel的操作方法的文章就介绍到这了,更多相关uni-app页面通信eventchannel内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com