gfdgd
gfdgd
  • 发布:2022-06-15 16:56
  • 更新:2022-09-19 19:46
  • 阅读:4720

vue3中 怎么在下个页面使用 getOpenerEventChannel()

分类:uni-app
2022-06-15 16:56 负责人:无 分享
已邀请:
DCloud_UNI_WZF

DCloud_UNI_WZF

这样试下

<script setup lang="ts">  
    import {onMounted,getCurrentInstance} from 'vue';  

    onMounted(() => {  
      const instance = getCurrentInstance().proxy  
      const eventChannel = instance.getOpenerEventChannel();  
      console.log('eventChannel',eventChannel)  
      eventChannel.emit('acceptDataFromOpenedPage', {data: 'data from test page'});  
      eventChannel.emit('someEvent', {data: 'data from test page for someEvent'});  
      // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据  
      eventChannel.on('acceptDataFromOpenerPage', function(data) {  
        console.log('acceptDataFromOpenerPage',data)  
      })  
    })  
</script>
  • gfdgd (作者)

    后期 会不会放到 @dcloudio/uni-app里面 类似import {onLoad,onShow} from "@dcloudio/uni-app"; 这样引入

    2022-06-16 10:15

  • DCloud_UNI_WZF

    回复 gfdgd: 不确定

    2022-06-16 10:21

  • gfdgd (作者)

    回复 DCloud_UNI_WZF: 那文档上应该说明吧 不然找都找不到 而且vue官方也不建议滥用getCurrentInstance

    https://v3.cn.vuejs.org/api/composition-api.html#getcurrentinstance

    2022-06-16 10:55

  • DCloud_UNI_WZF

    回复 gfdgd: 这属于vue3 使用过程中的东西,不会在 uni-app 文档中特殊说明

    2022-06-16 20:26

DCloud_UNI_WZF

DCloud_UNI_WZF

参考文档中的示例遇到什么问题呢?
HBuilderX 3.4.15 亲测没有问题。

  • gfdgd (作者)

    文档上是vue2的写法 vue3的写法是什么 vue3没有this 怎么获取getOpenerEventChannel

    2022-06-16 08:34

z***@gmail.com

z***@gmail.com

uni-app的vue3还有很长的路要走吧 我们都是还是用vue2的

  • gfdgd (作者)

    正在尝鲜 开发新的小程序

    2022-06-16 16:24

1***@qq.com

1***@qq.com


import { getCurrentInstance } from "vue";  
import {  onLoad } from "@dcloudio/uni-app";  
onLoad((option) => {  
const _this = getCurrentInstance();  
  const eventChannel = _this.ctx.getOpenerEventChannel();  
  eventChannel.on("sendMsgToSubPage", (data) => {  
    console.log("子页面接收到的数据", data);  
  });  
});  

该问题目前已经被锁定, 无法添加新回复