1# Web Component Development 2 3The **\<Web>** component can be used to display web pages. For details, see [Web](../reference/arkui-ts/ts-basic-components-web.md). 4 5## Creating a Component 6 7Create a **\<Web>** component in the .ets file under **main/ets/MainAbility/pages**. Then, in the created component, use **src** to specify the web page URI to be referenced, and bind a controller to the component to call the component APIs. 8 9 ```ts 10 // xxx.ets 11 import web_webview from '@ohos.web.webview'; 12 13 @Entry 14 @Component 15 struct WebComponent { 16 controller: web_webview.WebviewController = new web_webview.WebviewController(); 17 build() { 18 Column() { 19 Web({ src: 'https://www.example.com', controller: this.controller }) 20 } 21 } 22 } 23 ``` 24 25## Setting Styles and Attributes 26 27When using the **\<Web>** component, you must specify the styles and attributes. The sample code is as follows. 28 29```ts 30// xxx.ets 31import web_webview from '@ohos.web.webview'; 32 33@Entry 34@Component 35struct WebComponent { 36 fileAccess: boolean = true; 37 controller: web_webview.WebviewController = new web_webview.WebviewController(); 38 build() { 39 Column() { 40 Text('Hello world!') 41 .fontSize(20) 42 Web({ src: 'https://www.example.com', controller: this.controller }) 43 // Set the height and padding. 44 .height(500) 45 .padding(20) 46 // Set the file access permission and script execution permission. 47 .fileAccess(this.fileAccess) 48 .javaScriptAccess(true) 49 Text('End') 50 .fontSize(20) 51 } 52 } 53} 54``` 55## Adding Events and Methods 56 57Add the **onProgressChange** event for the **\<Web>** component, which is triggered when the loading progress changes and returns the progress value in its callback. Assign the progress value to the **\<Progress>** component to control the status of the component. When the progress reaches 100%, the **\<Progress>** component is hidden, and the web page loading is complete. 58 59```ts 60// xxx.ets 61import web_webview from '@ohos.web.webview'; 62 63@Entry 64@Component 65struct WebComponent { 66 @State progress: number = 0; 67 @State hideProgress: boolean = true; 68 fileAccess: boolean = true; 69 controller: web_webview.WebviewController = new web_webview.WebviewController(); 70 build() { 71 Column() { 72 Text('Hello world!') 73 .fontSize(20) 74 Progress({value: this.progress, total: 100}) 75 .color('#0000ff') 76 .visibility(this.hideProgress ? Visibility.None : Visibility.Visible) 77 Web({ src: 'https://www.example.com', controller: this.controller }) 78 .fileAccess(this.fileAccess) 79 .javaScriptAccess(true) 80 .height(500) 81 .padding(20) 82 // Add an onProgressChange event. 83 .onProgressChange(e => { 84 this.progress = e.newProgress; 85 // When the progress reaches 100%, the progress bar disappears. 86 if (this.progress === 100) { 87 this.hideProgress = true; 88 } else { 89 this.hideProgress = false; 90 } 91 }) 92 Text('End') 93 .fontSize(20) 94 } 95 } 96} 97``` 98Add the **runJavaScript** method to the **onPageEnd** event. The **onPageEnd** event is triggered when the web page finishes loading. In this case, the **runJavaScript** method can be used to execute JavaScript scripts in the HTML file. In the sample code below, when the web page finishes loading, the **onPageEnd** event is triggered to call the **test** method in the HTML file and print information on the console. 99 100```ts 101// xxx.ets 102import web_webview from '@ohos.web.webview'; 103 104@Entry 105@Component 106struct WebComponent { 107 @State progress: number = 0; 108 @State hideProgress: boolean = true; 109 @State webResult: string = '' 110 fileAccess: boolean = true; 111 // Define the controller for the \<Web> component. 112 controller: web_webview.WebviewController = new web_webview.WebviewController(); 113 build() { 114 Column() { 115 Text('Hello world!') 116 .fontSize(20) 117 Progress({value: this.progress, total: 100}) 118 .color('#0000ff') 119 .visibility(this.hideProgress ? Visibility.None : Visibility.Visible) 120 // Initialize the \<Web> component and bind it to the controller. 121 Web({ src: $rawfile('index.html'), controller: this.controller }) 122 .fileAccess(this.fileAccess) 123 .javaScriptAccess(true) 124 .height(500) 125 .padding(20) 126 .onProgressChange(e => { 127 this.progress = e.newProgress; 128 if (this.progress === 100) { 129 this.hideProgress = true; 130 } else { 131 this.hideProgress = false; 132 } 133 }) 134 .onPageEnd(e => { 135 // test() is defined in index.html. 136 try { 137 this.controller.runJavaScript( 138 'test()', 139 (error, result) => { 140 if (error) { 141 console.info(`run JavaScript error: ` + JSON.stringify(error)) 142 return; 143 } 144 if (result) { 145 this.webResult = result 146 console.info(`The test() return value is: ${result}`) 147 } 148 }); 149 console.info('url: ', e.url); 150 } catch (error) { 151 console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); 152 } 153 }) 154 Text('End') 155 .fontSize(20) 156 } 157 } 158} 159``` 160 161Create an HTML file in **main/resources/rawfile**. 162 163```html 164<!-- index.html --> 165<!DOCTYPE html> 166<html> 167<meta charset="utf-8"> 168<body> 169 Hello world! 170</body> 171<script type="text/javascript"> 172 function test() { 173 console.info('Ark Web Component'); 174 } 175</script> 176</html> 177``` 178 179## Enabling Web Debugging 180To debug web pages in the **\<Web>** component on a device, connect the device to a PC through the USB port, and then set the **webDebuggingAccess** attribute of the **\<Web>** component to **true** and enable port forwarding on the PC. 181 182The configuration procedure is as follows: 183 1841. Set the **webDebuggingAccess** attribute of the **\<Web>** component to **true**. 185 ```ts 186 // xxx.ets 187 import web_webview from '@ohos.web.webview'; 188 189 @Entry 190 @Component 191 struct WebComponent { 192 controller: web_webview.WebviewController = new web_webview.WebviewController(); 193 build() { 194 Column() { 195 Web({ src: 'www.example.com', controller: this.controller }) 196 .webDebuggingAccess(true) // true means to enable web debugging. 197 } 198 } 199 } 200 ``` 201 2022. Enable port forwarding on the PC and add a mapping for TCP port 9222. 203 ```ts 204 hdc fport tcp:9222 tcp:9222 205 ``` 206 You can run the following command to view the existing mapping table: 207 ```ts 208 hdc fport ls 209 ``` 210When you are done, you can debug a web page as follows: On the target device, open the **\<Web>** component in the application and access the web page to debug; on the PC, use the Chrome browser to access **http://localhost:9222** and start to debug the web page. 211 212## Scenario Example 213 214In this example, you'll implement a **\<Web>** component where videos can be played dynamically. Embed a video resource into an HTML page, and then use the **\<Web>** component controller to invoke the **onActive** and **onInactive** methods to activate and pause page rendering, respectively. Upon clicking of the **onInactive** button, the **\<Web>** component stops rendering and the video playback pauses. Upon clicking of the **onActive** button, the **\<Web>** component is activated and the video playback resumes. 215 216 ```ts 217 // xxx.ets 218import web_webview from '@ohos.web.webview'; 219 220@Entry 221@Component 222struct WebComponent { 223 controller: web_webview.WebviewController = new web_webview.WebviewController(); 224 build() { 225 Column() { 226 Row() { 227 Button('onActive').onClick(() => { 228 console.info("Web Component onActive"); 229 try { 230 this.controller.onActive(); 231 } catch (error) { 232 console.error(`Errorcode: ${error.code}, Message: ${error.message}`); 233 } 234 }) 235 Button('onInactive').onClick(() => { 236 console.info("Web Component onInactive"); 237 try { 238 this.controller.onInactive(); 239 } catch (error) { 240 console.error(`Errorcode: ${error.code}, Message: ${error.message}`); 241 } 242 }) 243 } 244 Web({ src: $rawfile('index.html'), controller: this.controller }) 245 .fileAccess(true) 246 } 247 } 248} 249 ``` 250 251 ```html 252 <!-- index.html --> 253 <!DOCTYPE html> 254 <html> 255 <meta charset="utf-8"> 256 <body> 257 <video width="320" height="240" controls="controls" muted="muted" autoplay="autoplay"> 258 <!-- The test.mp4 video file is stored in main/resources/rawfile. --> 259 <source src="test.mp4" type="video/mp4"> 260 </video> 261 </body> 262 </html> 263 ``` 264