s***@qq.com
s***@qq.com
  • 发布:2022-04-02 11:25
  • 更新:2024-03-02 09:19
  • 阅读:5292

vue3 组合式 api 中使用 eventChannel(getOpenerEventChannel)

分类:uni-app
<script setup>  
import { getCurrentInstance } from 'vue'  
import { onLoad } from '@dcloudio/uni-app'  

const _this = getCurrentInstance().appContext.config.globalProperties  

onLoad(options => {  
  console.log('page onLoad: ', options)  

  const eventChannel = _this.getOpenerEventChannel()  
  eventChannel.on('acceptDataFromOpenerPage', data => {  
    console.log('acceptDataFromOpenerPage: ', data)  
  })  
})  
</script>
2 关注 分享
i***@gmail.com key_

要回复文章请先登录注册

1***@163.com

1***@163.com

如何解决ts报错问题
2024-03-02 09:19
8***@qq.com

8***@qq.com

回复 亲爱的小程程 :
可以通过编译,但是 ts 报错
2023-09-02 12:04
亲爱的小程程

亲爱的小程程

``` javascript
<script setup>

import { getCurrentInstance } from 'vue'

import { onLoad } from '@dcloudio/uni-app'


// const _this = getCurrentInstance().appContext.config.globalProperties

const { proxy } = getCurrentInstance();

const _this = proxy;

onLoad(options => {

console.log('page onLoad: ', options)


const eventChannel = _this.getOpenerEventChannel()

eventChannel.on('acceptDataFromOpenerPage', data => {

console.log('acceptDataFromOpenerPage: ', data)

})

})

</script>
```
2023-07-10 18:53
亲爱的小程程

亲爱的小程程

<script setup>
import { getCurrentInstance } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

// const _this = getCurrentInstance().appContext.config.globalProperties
const { proxy } = getCurrentInstance();
const _this = proxy;
onLoad(options => {
console.log('page onLoad: ', options)

const eventChannel = _this.getOpenerEventChannel()
eventChannel.on('acceptDataFromOpenerPage', data => {
console.log('acceptDataFromOpenerPage: ', data)
})
})
</script>
2023-07-10 18:52
7***@qq.com

7***@qq.com

回复 1***@qq.com :
Cannot read property 'ctx' of null
2023-06-16 16:08
高级CV

高级CV

回复 1***@qq.com :
onLoad((option) => {
const _this = getCurrentInstance()
const eventChannel = _this.ctx.getOpenerEventChannel()
eventChannel.on('sendMsgToSubPage', (data) => {
console.log('子页面接收到的数据', data)
})
})
在ts 里不能用了
2023-02-02 11:00
key_

key_

APP端:Uncaught TypeError: _this.getOpenerEventChannel is not a function
2022-11-12 09:05
zwp_jiayou

zwp_jiayou

回复 1***@qq.com :
这个才是对的哈哈
2022-11-07 11:10
1***@qq.com

1***@qq.com

```javascript

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);
});
});


```
2022-09-19 19:48
a***@qq.com

a***@qq.com

貌似不行
2022-06-10 14:34