提交 7aa23ec3 编写于 作者: X xiongjun_gitee
上级 c46bdd33
...@@ -1468,12 +1468,10 @@ struct WebComponent { ...@@ -1468,12 +1468,10 @@ struct WebComponent {
Button('postMessage') Button('postMessage')
.onClick(() => { .onClick(() => {
try { try {
// 1、创建两个消息端口 // 1、创建两个消息端口
this.ports = this.controller.createWebMessagePorts(); this.ports = this.controller.createWebMessagePorts();
// 2、将其中一个消息端口发送到HTML侧,由HTML侧保存并使用。 // 2、在应用侧的消息端口(如端口1)上注册回调事件。
this.controller.postMessage('__init_port__', [this.ports[0]], '*'); this.ports[1].onMessageEvent((result: web_webview.WebMessage) => {
// 3、另一个消息端口在应用侧注册回调事件。
this.ports[1].onMessageEvent((result: WebMessage) => {
var msg = 'Got msg from HTML:'; var msg = 'Got msg from HTML:';
if (typeof(result) == "string") { if (typeof(result) == "string") {
console.log("received string message from html5, string is:" + result); console.log("received string message from html5, string is:" + result);
...@@ -1490,6 +1488,8 @@ struct WebComponent { ...@@ -1490,6 +1488,8 @@ struct WebComponent {
} }
this.receivedFromHtml = msg; this.receivedFromHtml = msg;
}) })
// 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。
this.controller.postMessage('__init_port__', [this.ports[0]], '*');
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
} }
...@@ -1527,7 +1527,7 @@ struct WebComponent { ...@@ -1527,7 +1527,7 @@ struct WebComponent {
<h1>WebView Message Port Demo</h1> <h1>WebView Message Port Demo</h1>
<div> <div>
<input type="button" value="SendToEts" onclick="PostMsgToEts(msgFromJS.value);"/><br/> <input type="button" value="SendToEts" onclick="PostMsgToEts(msgFromJS.value);"/><br/>
<input id="msgFromJs" type="text" value="send this message from HTML to ets"/><br/> <input id="msgFromJS" type="text" value="send this message from HTML to ets"/><br/>
</div> </div>
<p class="output">display received message send from ets</p> <p class="output">display received message send from ets</p>
</body> </body>
...@@ -4295,6 +4295,10 @@ storeWebArchive(baseName: string, autoName: boolean): Promise\<string> ...@@ -4295,6 +4295,10 @@ storeWebArchive(baseName: string, autoName: boolean): Promise\<string>
web组件地理位置权限管理对象。 web组件地理位置权限管理对象。
### 需要权限
访问地理位置时需添加权限:ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION、ohos.permission.LOCATION_IN_BACKGROUND,具体权限说明请参考[位置服务](./js-apis-geolocation.md)
### allowGeolocation ### allowGeolocation
static allowGeolocation(origin: string): void static allowGeolocation(origin: string): void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册