提交 0a265de0 编写于 作者: X xiongjun_gitee

update js-apis-webview

Signed-off-by: Nxiongjun_gitee <xiongjun28@huawei.com>
上级 bdc9a196
...@@ -1508,12 +1508,10 @@ struct WebComponent { ...@@ -1508,12 +1508,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);
...@@ -1530,6 +1528,8 @@ struct WebComponent { ...@@ -1530,6 +1528,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}`);
} }
...@@ -1545,7 +1545,7 @@ struct WebComponent { ...@@ -1545,7 +1545,7 @@ struct WebComponent {
console.error(`ports is null, Please initialize first`); 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}`);
} }
}) })
Web({ src: $rawfile('xxx.html'), controller: this.controller }) Web({ src: $rawfile('xxx.html'), controller: this.controller })
...@@ -1567,7 +1567,7 @@ struct WebComponent { ...@@ -1567,7 +1567,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>
...@@ -4335,6 +4335,10 @@ storeWebArchive(baseName: string, autoName: boolean): Promise\<string> ...@@ -4335,6 +4335,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.
先完成此消息的编辑!
想要评论请 注册