未验证 提交 0b2cf0da 编写于 作者: O openharmony_ci 提交者: Gitee

!11373 【web子系统】更新webMessagePort资料样例,添加判空处理

Merge pull request !11373 from echoorchid/master
...@@ -46,7 +46,11 @@ struct WebComponent { ...@@ -46,7 +46,11 @@ struct WebComponent {
Column() { Column() {
Button('close') Button('close')
.onClick(() => { .onClick(() => {
this.msgPort[1].close(); if (this.msgPort && this.msgPort[1]) {
this.msgPort[1].close();
} else {
console.error("msgPort is null, Please initialize first");
}
}) })
Web({ src: 'www.example.com', controller: this.controller }) Web({ src: 'www.example.com', controller: this.controller })
} }
...@@ -1383,7 +1387,11 @@ struct WebComponent { ...@@ -1383,7 +1387,11 @@ struct WebComponent {
Button('SendDataToHTML') Button('SendDataToHTML')
.onClick(() => { .onClick(() => {
try { try {
this.ports[1].postMessageEvent("post message from ets to HTML"); if (this.ports && this.ports[1]) {
this.ports[1].postMessageEvent("post message from ets to HTML");
} else {
console.error(`ports is null, Please initialize first`);
}
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
} }
...@@ -1434,7 +1442,11 @@ window.addEventListener('message', function (event) { ...@@ -1434,7 +1442,11 @@ window.addEventListener('message', function (event) {
// 3. 使用h5Port往ets侧发送消息. // 3. 使用h5Port往ets侧发送消息.
function PostMsgToEts(data) { function PostMsgToEts(data) {
h5Port.postMessage(data); if (h5Port) {
h5Port.postMessage(data);
} else {
console.error("h5Port is null, Please initialize first");
}
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册