面板小程序中eventChannel怎么用?

小程序开发相关产品技术讨论,包括面板、智能小程序、React Native、Ray跨端框架、Panel SDK、微信小程序、小程序开发工具(IDE)及其他开发技术相关等话题


Post Reply
Mical
Posts: 22

我的程序是在模版public-sdm的基础上改出来的,问了AI助手,这个问题,AI助手回答的

在子页面:

Page({
onLoad(options) {
// 获取 eventChannel
const eventChannel = this.getOpenerEventChannel();
// 在某个操作完成以后,发送数据回父页面
eventChannel.emit('acceptDataFromOpenedPage', { data: 'some data' });
}
});

在父页面:
ty.navigateTo({
url: 'path_to_your_page',
success: function (res) {
// 监听从子页面发回的数据
res.eventChannel.on('acceptDataFromOpenedPage', function (data) {
console.log(data); // 输出:{ data: 'some data' }
});
}
});
父页面这个配置我会,但是子页面这个,实在是不知道怎么搞。我的添加方式:
usePageEvent('onLoad', () => {
hideMenuButton();
const eventChannel = this.getOpenerEventChannel();
eventChannel.emit('acceptDataFromSchedulePage', { data: 'some data' });
});

报错了,子页面和父页面都报错了。应该是没有正确的引入吧,接口找不到。谁能举例说明一下?

谢谢!


Tags:
noyobo
Posts: 17

Re: 面板小程序中eventChannel怎么用?

Mical
Posts: 22

Re: 面板小程序中eventChannel怎么用?

参照这个链接的办法,我解决了页面句柄的问题。编译也通过了。但是运行起来后,

eventChannel.emmit 和 eventChannel.on 这个两个接口都没有找到,报错了。

noyobo 2024年 Dec 13日 17:59

https://developer.tuya.com/cn/miniapp/d ... geinstance

参考这个。

lshinylee
Posts: 316

Re: 面板小程序中eventChannel怎么用?

贴个代码使用示例或者附件看看呢?

:D :D :D

Mical
Posts: 22

Re: 面板小程序中eventChannel怎么用?

父页面:
const jumpUrl = /pages/doodleEdit/index?index=${index}&data=${jsonString};
console.log("jumpToEditDoodlePage jumpUrl = " + jumpUrl);
navigateTo({
url: jumpUrl,
success: function(res) {
console.log('success res = ', res);
// 获取 EventChannel 对象
const eventChannel = res.eventChannel;


Code: Select all

    // 监听来自子页面的事件,并获取数据
    eventChannel.on('acceptDataFromChildPage', function(data) {
      console.log('来自子页面的数据:', data);
    });
  },
});

子页面:
import { usePageEvent, usePageInstance} from '@ray-js/ray';
const page = usePageInstance();
usePageEvent('onLoad', (query) => {
const eventChannel = page.getOpenerEventChannel();
console.log("usePageEvent onLoad:", eventChannel);
// 当需要向父页面传递数据时
eventChannel.emit('sendDataToParent', { data: 'some data' });
});

目前consolo报错是 eventChannel.on 和eventChannel.emit 这两个方法找不到。

index.tsx:235 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'on')
at success (index.tsx:235:22)
at success (service.js?side=service:3:376036)
at service.js?side=service:3:492305
at Object.invoke (C:\Users\19195\AppData\Local\Programs\Tuya MiniApp IDE\resources\app\node_modules\@ark\miniapp-bridge\dist\electron-browser\mothra-bridge.js:1:413)
success @ index.tsx:235
success @ service.js?side=service:3
(anonymous) @ service.js?side=service:3
invoke @ C:\Users\19195\AppData\Local\Programs\Tuya MiniApp IDE\resources\app\node_modules\@ark\miniapp-bridge\dist\electron-browser\mothra-bridge.js:1
await in invoke (async)
value @ service.js?side=service:3
Af @ service.js?side=service:3
navigateTo @ service.js?side=service:3
e.navigateTo @ service.js?side=service:3
jumpToEditDoodlePage @ index.tsx:227
onClick @ index.tsx:266
handler @ createCallbackProxy.js:66
(anonymous) @ Container.js:164
batchedUpdates @ react-reconciler.development.js:14923
cbOrEventHandler @ Container.js:163
Fr @ service.js?side=service:3
value @ service.js?side=service:3
value @ service.js?side=service:3
value @ service.js?side=service:3
$emit @ basic.js:7
onClick @ index.component.js:72
Fr @ service.js?side=service:3
value @ service.js?side=service:3
i @ service.js?side=service:3
setTimeout (async)
ve.setTimeout @ service.js?side=service:3
gt @ service.js?side=service:3
(anonymous) @ service.js?side=service:3
(anonymous) @ service.js?side=service:3
value @ service.js?side=service:3
(anonymous) @ service.js?side=service:3
value @ service.js?side=service:3
(anonymous) @ service.js?side=service:3
(anonymous) @ C:\Users\19195\AppData\Local\Programs\Tuya MiniApp IDE\resources\app\node_modules\@ark\miniapp-bridge\dist\electron-browser\mothra-bridge.js:1
__handle @ C:\Users\19195\AppData\Local\Programs\Tuya MiniApp IDE\resources\app\node_modules\@ark\miniapp-bridge\dist\electron-browser\mothra-bridge.js:1
(anonymous) @ VM422:1
(anonymous) @ node:electron/js2c/renderer_init:2
(anonymous) @ node:electron/js2c/renderer_init:2
emit @ node:events:513
onMessage @ node:electron/js2c/renderer_init:2
service.js?side=service:3 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emit')
at index.tsx:78:18
at createPageConfig.js:201:37
at batchedUpdates (react-reconciler.development.js:14923:12)
at createPageConfig.js:200:26
at Array.map (<anonymous>)
at Caches2.callLifecycle (createPageConfig.js:199:57)
at Caches2.<anonymous> (createPageConfig.js:153:34)
at step (createPageConfig.js:32:23)
at Object.next (createPageConfig.js:13:53)
at fulfilled (createPageConfig.js:4:58)

Lucy
Posts: 12

Re: 面板小程序中eventChannel怎么用?

看下基础库版本是不是最新的, 使用最新的基础库版本

Mical
Posts: 22

Re: 面板小程序中eventChannel怎么用?

附件是我使用的配置截图,请帮我看看要调整哪一个配置来尝试。谢谢!

Attachments
项目配置.png
IDE 配置.png
Mical
Posts: 22

Re: 面板小程序中eventChannel怎么用?

我把BaseKit调整成最新版本尝试过,依然报同样的错。

Lucy 2025年 Jan 10日 10:34

看下基础库版本是不是最新的, 使用最新的基础库版本

Lucy
Posts: 12

Re: 面板小程序中eventChannel怎么用?

navigateTo 属性里要有 events, 要不就没有 res.eventChannel 对象的

Post Reply