uniapp 页面通信

navigateTo

js 复制代码
// 在起始页面跳转到test.vue页面,并监听test.vue发送过来的事件数据
uni.navigateTo({
  url: '/pages/test?id=1',
  events: {
    // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
    acceptDataFromOpenedPage: function(data) {
      console.log(data)
    },
    ...
  },
  success: function(res) {
    // 通过eventChannel向被打开页面传送数据
    res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'data from starter page' })
  }
})

注意:vue3 与 vue 2 被打开页面初始化 略有不同

vue2

js 复制代码
onLoad: function(option) {
  const eventChannel = this.getOpenerEventChannel();
  // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
  eventChannel.on('acceptDataFromOpenerPage', function(data) {
    console.log(data)
  })
}

Vue3

js 复制代码
// 在test.vue页面,向起始页通过事件传递数据
import { onLoad } from '@dcloudio/uni-app'
import { getCurrentInstance, ref } from 'vue'
const _this = getCurrentInstance().proxy
onLoad(() => {
  let eventChannel = _this.getOpenerEventChannel()
  // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
  eventChannel.on('acceptDataFromOpenerPage', (data) => {
  	console.log(data)
  })
})

被打开页面,向打开页面传递消息,需要将 eventChannel 缓存,并在对应时刻,调用 eventChannel.emit

js 复制代码
<script setup>
let eventChannel = null

onLoad(() => {
  // xxx
  eventChannel = _this.getOpenerEventChannel()
})
const submit = () => {
  eventChannel.emit('onConfirm', {})
  uni.navigateBack()
}
</script>
相关推荐
遇到困难睡大觉哈哈3 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569153 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43484 小时前
前端在移动端中的网络请求优化
前端
g***B7384 小时前
前端在移动端中的Ionic
前端
游戏开发爱好者84 小时前
iOS 开发者的安全加固工具,从源码到成品 IPA 的多层防护体系实践
android·安全·ios·小程序·uni-app·cocoa·iphone
大猩猩X4 小时前
vxe-gantt 甘特图使用右键菜单
vue.js·vxe-table·vxe-ui·vxe-gantt
拿破轮4 小时前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin4 小时前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aerelin5 小时前
爬虫playwright入门讲解
前端·javascript·html·playwright
5***o5005 小时前
前端在移动端中的NativeBase
前端