未验证 提交 6e1352c5 编写于 作者: O openharmony_ci 提交者: Gitee

!14166 修改web开发组件示例代码中已废弃的WebController为WebviewController

Merge pull request !14166 from zhufenghao/master
...@@ -8,10 +8,12 @@ Web是提供网页显示能力的组件,具体用法请参考 [Web API](../ref ...@@ -8,10 +8,12 @@ Web是提供网页显示能力的组件,具体用法请参考 [Web API](../ref
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
controller: WebController = new WebController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Web({ src: 'https://www.example.com', controller: this.controller }) Web({ src: 'https://www.example.com', controller: this.controller })
...@@ -26,11 +28,13 @@ Web组件的使用需要添加丰富的页面样式和功能属性。设置heigh ...@@ -26,11 +28,13 @@ Web组件的使用需要添加丰富的页面样式和功能属性。设置heigh
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
fileAccess: boolean = true; fileAccess: boolean = true;
controller: WebController = new WebController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Text('Hello world!') Text('Hello world!')
...@@ -54,13 +58,15 @@ struct WebComponent { ...@@ -54,13 +58,15 @@ struct WebComponent {
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
@State progress: number = 0; @State progress: number = 0;
@State hideProgress: boolean = true; @State hideProgress: boolean = true;
fileAccess: boolean = true; fileAccess: boolean = true;
controller: WebController = new WebController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Text('Hello world!') Text('Hello world!')
...@@ -93,14 +99,17 @@ struct WebComponent { ...@@ -93,14 +99,17 @@ struct WebComponent {
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
@State progress: number = 0; @State progress: number = 0;
@State hideProgress: boolean = true; @State hideProgress: boolean = true;
@State webResult: string = ''
fileAccess: boolean = true; fileAccess: boolean = true;
// 定义Web组件的控制器controller // 定义Web组件的控制器controller
controller: WebController = new WebController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Text('Hello world!') Text('Hello world!')
...@@ -124,8 +133,23 @@ struct WebComponent { ...@@ -124,8 +133,23 @@ struct WebComponent {
}) })
.onPageEnd(e => { .onPageEnd(e => {
// test()在index.html中定义 // test()在index.html中定义
this.controller.runJavaScript({ script: 'test()' }); try {
this.controller.runJavaScript(
'test()',
(error, result) => {
if (error) {
console.info(`run JavaScript error: ` + JSON.stringify(error))
return;
}
if (result) {
this.webResult = result
console.info(`The test() return value is: ${result}`)
}
});
console.info('url: ', e.url); console.info('url: ', e.url);
} catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
}
}) })
Text('End') Text('End')
.fontSize(20) .fontSize(20)
...@@ -160,10 +184,12 @@ struct WebComponent { ...@@ -160,10 +184,12 @@ struct WebComponent {
1、首先设置Web组件属性webDebuggingAccess为true。 1、首先设置Web组件属性webDebuggingAccess为true。
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
controller: WebController = new WebController() controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Web({ src: 'www.example.com', controller: this.controller }) Web({ src: 'www.example.com', controller: this.controller })
...@@ -189,20 +215,30 @@ struct WebComponent { ...@@ -189,20 +215,30 @@ struct WebComponent {
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview';
@Entry @Entry
@Component @Component
struct WebComponent { struct WebComponent {
controller: WebController = new WebController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
build() { build() {
Column() { Column() {
Row() { Row() {
Button('onActive').onClick(() => { Button('onActive').onClick(() => {
console.info("Web Component onActive"); console.info("Web Component onActive");
try {
this.controller.onActive(); this.controller.onActive();
} catch (error) {
console.error(`Errorcode: ${error.code}, Message: ${error.message}`);
}
}) })
Button('onInactive').onClick(() => { Button('onInactive').onClick(() => {
console.info("Web Component onInactive"); console.info("Web Component onInactive");
try {
this.controller.onInactive(); this.controller.onInactive();
} catch (error) {
console.error(`Errorcode: ${error.code}, Message: ${error.message}`);
}
}) })
} }
Web({ src: $rawfile('index.html'), controller: this.controller }) Web({ src: $rawfile('index.html'), controller: this.controller })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册