未验证 提交 0505e188 编写于 作者: O openharmony_ci 提交者: Gitee

!23503 web ArkTs规范整改( zh-cn/application-dev/web/ )

Merge pull request !23503 from zgit2021/main
...@@ -12,12 +12,13 @@ ...@@ -12,12 +12,13 @@
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
ports: web_webview.WebMessagePort[]; ports: web_webview.WebMessagePort[] = [];
@State sendFromEts: string = 'Send this message from ets to HTML'; @State sendFromEts: string = 'Send this message from ets to HTML';
@State receivedFromHtml: string = 'Display received message send from HTML'; @State receivedFromHtml: string = 'Display received message send from HTML';
...@@ -57,7 +58,8 @@ ...@@ -57,7 +58,8 @@
// 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。 // 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。
this.controller.postMessage('__init_port__', [this.ports[0]], '*'); this.controller.postMessage('__init_port__', [this.ports[0]], '*');
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
...@@ -71,7 +73,8 @@ ...@@ -71,7 +73,8 @@
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}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
Web({ src: $rawfile('xxx.html'), controller: this.controller }) Web({ src: $rawfile('xxx.html'), controller: this.controller })
......
...@@ -13,6 +13,7 @@ Web组件提供了WebCookieManager类,用于管理Web组件的Cookie信息。C ...@@ -13,6 +13,7 @@ Web组件提供了WebCookieManager类,用于管理Web组件的Cookie信息。C
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
...@@ -26,7 +27,8 @@ struct WebComponent { ...@@ -26,7 +27,8 @@ struct WebComponent {
try { try {
web_webview.WebCookieManager.setCookie('https://www.example.com', 'value=test'); web_webview.WebCookieManager.setCookie('https://www.example.com', 'value=test');
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
Web({ src: 'www.example.com', controller: this.controller }) Web({ src: 'www.example.com', controller: this.controller })
...@@ -82,6 +84,7 @@ struct WebComponent { ...@@ -82,6 +84,7 @@ struct WebComponent {
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
...@@ -96,7 +99,8 @@ struct WebComponent { ...@@ -96,7 +99,8 @@ struct WebComponent {
// 设置为true时同时清除rom和ram中的缓存,设置为false时只清除ram中的缓存 // 设置为true时同时清除rom和ram中的缓存,设置为false时只清除ram中的缓存
this.controller.removeCache(true); this.controller.removeCache(true);
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
Web({ src: 'www.example.com', controller: this.controller }) Web({ src: 'www.example.com', controller: this.controller })
......
...@@ -12,6 +12,7 @@ Web组件支持前端页面选择文件上传功能,应用开发者可以使 ...@@ -12,6 +12,7 @@ Web组件支持前端页面选择文件上传功能,应用开发者可以使
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
...@@ -25,7 +26,9 @@ Web组件支持前端页面选择文件上传功能,应用开发者可以使 ...@@ -25,7 +26,9 @@ Web组件支持前端页面选择文件上传功能,应用开发者可以使
let fileList: Array<string> = [ let fileList: Array<string> = [
'xxx/test.png', 'xxx/test.png',
] ]
event.result.handleFileList(fileList) if (event) {
event.result.handleFileList(fileList)
}
return true; return true;
}) })
} }
......
...@@ -53,17 +53,23 @@ Web组件提供位置权限管理能力。开发者可以通过[onGeolocationSho ...@@ -53,17 +53,23 @@ Web组件提供位置权限管理能力。开发者可以通过[onGeolocationSho
primaryButton: { primaryButton: {
value: 'cancel', value: 'cancel',
action: () => { action: () => {
event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 if (event) {
event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
}
} }
}, },
secondaryButton: { secondaryButton: {
value: 'ok', value: 'ok',
action: () => { action: () => {
event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求 if (event) {
event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求
}
} }
}, },
cancel: () => { cancel: () => {
event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 if (event) {
event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
}
} }
}) })
}) })
......
...@@ -16,16 +16,21 @@ ...@@ -16,16 +16,21 @@
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
class testClass {
constructor() {
}
test(): string {
return 'ArkTS Hello World!';
}
}
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
webviewController: web_webview.WebviewController = new web_webview.WebviewController(); webviewController: web_webview.WebviewController = new web_webview.WebviewController();
// 声明需要注册的对象 // 声明需要注册的对象
testObj = { @State testObj: testClass = new testClass();
test: () => {
return 'ArkTS Hello World!';
}
}
build() { build() {
Column() { Column() {
...@@ -49,19 +54,26 @@ ...@@ -49,19 +54,26 @@
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
class testClass {
constructor() {
}
test(): string {
return "ArkUI Web Component";
}
toString(): void {
console.log('Web Component toString');
}
}
@Entry @Entry
@Component @Component
struct Index { struct Index {
webviewController: web_webview.WebviewController = new web_webview.WebviewController(); webviewController: web_webview.WebviewController = new web_webview.WebviewController();
testObj = { @State testObj: testClass = new testClass();
test: (data) => {
return "ArkUI Web Component";
},
toString: () => {
console.info('Web Component toString');
}
}
build() { build() {
Column() { Column() {
...@@ -70,7 +82,8 @@ ...@@ -70,7 +82,8 @@
try { try {
this.webviewController.refresh(); this.webviewController.refresh();
} catch (error) { } catch (error) {
console.error(`Errorcode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
Button('Register JavaScript To Window') Button('Register JavaScript To Window')
...@@ -78,7 +91,8 @@ ...@@ -78,7 +91,8 @@
try { try {
this.webviewController.registerJavaScriptProxy(this.testObj, "objName", ["test", "toString"]); this.webviewController.registerJavaScriptProxy(this.testObj, "objName", ["test", "toString"]);
} catch (error) { } catch (error) {
console.error(`Errorcode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
Web({ src: $rawfile('index.html'), controller: this.webviewController }) Web({ src: $rawfile('index.html'), controller: this.webviewController })
......
...@@ -31,7 +31,7 @@ Web组件提供了在新窗口打开页面的能力,开发者可以通过[mult ...@@ -31,7 +31,7 @@ Web组件提供了在新窗口打开页面的能力,开发者可以通过[mult
.multiWindowAccess(true) .multiWindowAccess(true)
.onWindowNew((event) => { .onWindowNew((event) => {
console.info("onWindowNew..."); console.info("onWindowNew...");
var popController: web_webview.WebviewController = new web_webview.WebviewController(); let popController: web_webview.WebviewController = new web_webview.WebviewController();
// 开发者需要在此处新建窗口,跟popController关联,并且将popController返回给Web组件。如果不需要打开新窗口请将返回值设置为event.handler.setWebController(null); // 开发者需要在此处新建窗口,跟popController关联,并且将popController返回给Web组件。如果不需要打开新窗口请将返回值设置为event.handler.setWebController(null);
event.handler.setWebController(popController); event.handler.setWebController(popController);
}) })
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
...@@ -33,7 +34,8 @@ struct WebComponent { ...@@ -33,7 +34,8 @@ struct WebComponent {
// 点击按钮时,通过loadUrl,跳转到www.example1.com // 点击按钮时,通过loadUrl,跳转到www.example1.com
this.webviewController.loadUrl('www.example1.com'); this.webviewController.loadUrl('www.example1.com');
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
// 组件创建时,加载www.example.com // 组件创建时,加载www.example.com
...@@ -64,6 +66,7 @@ struct WebComponent { ...@@ -64,6 +66,7 @@ struct WebComponent {
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
...@@ -78,7 +81,8 @@ struct WebComponent { ...@@ -78,7 +81,8 @@ struct WebComponent {
// 点击按钮时,通过loadUrl,跳转到local1.html // 点击按钮时,通过loadUrl,跳转到local1.html
this.webviewController.loadUrl($rawfile("local1.html")); this.webviewController.loadUrl($rawfile("local1.html"));
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
// 组件创建时,通过$rawfile加载本地文件local.html // 组件创建时,通过$rawfile加载本地文件local.html
...@@ -111,6 +115,7 @@ Web组件可以通过[loadData()](../reference/apis/js-apis-webview.md#loaddata) ...@@ -111,6 +115,7 @@ Web组件可以通过[loadData()](../reference/apis/js-apis-webview.md#loaddata)
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Entry @Entry
@Component @Component
...@@ -129,7 +134,8 @@ struct WebComponent { ...@@ -129,7 +134,8 @@ struct WebComponent {
'UTF-8' 'UTF-8'
); );
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); let e: business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
} }
}) })
// 组件创建时,加载www.example.com // 组件创建时,加载www.example.com
......
...@@ -21,7 +21,6 @@ struct WebComponent { ...@@ -21,7 +21,6 @@ struct WebComponent {
.onClick(() => { .onClick(() => {
if (this.webviewController.accessBackward()) { if (this.webviewController.accessBackward()) {
this.webviewController.backward(); this.webviewController.backward();
return true;
} }
}) })
Web({ src: 'https://www.example.com/cn/', controller: this.webviewController}) Web({ src: 'https://www.example.com/cn/', controller: this.webviewController})
...@@ -55,11 +54,13 @@ struct WebComponent { ...@@ -55,11 +54,13 @@ struct WebComponent {
Column() { Column() {
Web({ src: $rawfile('route.html'), controller: this.webviewController }) Web({ src: $rawfile('route.html'), controller: this.webviewController })
.onUrlLoadIntercept((event) => { .onUrlLoadIntercept((event) => {
let url: string = event.data as string; if (event) {
if (url.indexOf('native://') === 0) { let url: string = event.data as string;
// 跳转其他界面 if (url.indexOf('native://') === 0) {
router.pushUrl({ url:url.substring(9) }) // 跳转其他界面
return true; router.pushUrl({ url:url.substring(9) })
return true;
}
} }
return false; return false;
}) })
...@@ -122,18 +123,20 @@ Web组件可以实现点击前端页面超链接跳转到其他应用。 ...@@ -122,18 +123,20 @@ Web组件可以实现点击前端页面超链接跳转到其他应用。
Column() { Column() {
Web({ src: $rawfile('xxx.html'), controller: this.webviewController}) Web({ src: $rawfile('xxx.html'), controller: this.webviewController})
.onUrlLoadIntercept((event) => { .onUrlLoadIntercept((event) => {
let url: string = event.data as string; if (event) {
// 判断链接是否为拨号链接 let url: string = event.data as string;
if (url.indexOf('tel://') === 0) { // 判断链接是否为拨号链接
// 跳转拨号界面 if (url.indexOf('tel://') === 0) {
call.makeCall(url.substring(6), (err) => { // 跳转拨号界面
if (!err) { call.makeCall(url.substring(6), (err) => {
console.info('make call succeeded.'); if (!err) {
} else { console.info('make call succeeded.');
console.info('make call fail, err is:' + JSON.stringify(err)); } else {
} console.info('make call fail, err is:' + JSON.stringify(err));
}); }
return true; });
return true;
}
} }
return false; return false;
}) })
......
...@@ -51,10 +51,12 @@ Web网页上发起资源加载请求,应用层收到资源请求消息。应 ...@@ -51,10 +51,12 @@ Web网页上发起资源加载请求,应用层收到资源请求消息。应
Column() { Column() {
Web({ src: $rawfile('example.html'), controller: this.controller }) Web({ src: $rawfile('example.html'), controller: this.controller })
.onInterceptRequest((event) => { .onInterceptRequest((event) => {
console.info('url:' + event.request.getRequestUrl()); if (event) {
// 拦截页面请求 console.info('url:' + event.request.getRequestUrl());
if (event.request.getRequestUrl() !== 'https://www.intercept.com/test.html') { // 拦截页面请求
return null; if (event.request.getRequestUrl() !== 'https://www.intercept.com/test.html') {
return this.responseResource;
}
} }
// 构造响应数据 // 构造响应数据
this.responseResource.setResponseData(this.webData); this.responseResource.setResponseData(this.webData);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册