1# 前端页面调用应用侧函数 2<!--Kit: ArkWeb--> 3<!--Subsystem: Web--> 4<!--Owner: @aohui--> 5<!--Designer: @yaomingliu--> 6<!--Tester: @ghiker--> 7<!--Adviser: @HelloCrease--> 8 9开发者使用Web组件将应用侧代码注册到前端页面中,注册完成之后,前端页面中使用注册的对象名称就可以调用应用侧的方法,实现在前端页面中调用应用侧方法。 10 11## 如何建立应用侧与H5侧的交互通道 12 13注册应用侧代码有两种方式,一种在Web组件初始化调用,使用[javaScriptProxy()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#javascriptproxy)接口。另外一种在Web组件初始化完成后调用,使用[registerJavaScriptProxy()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#registerjavascriptproxy)接口。两种方式都需要和[deleteJavaScriptRegister](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#deletejavascriptregister)接口配合使用,防止内存泄漏。 14 15在下面的示例中,将test()方法注册在前端页面中, 该函数可以在前端页面触发运行。 16 17 18- [javaScriptProxy()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#javascriptproxy)接口使用示例如下。 19 20 ```ts 21 // xxx.ets 22 import { webview } from '@kit.ArkWeb'; 23 import { BusinessError } from '@kit.BasicServicesKit'; 24 25 class TestClass { 26 constructor() { 27 } 28 29 test(): string { 30 return 'ArkTS Hello World!'; 31 } 32 } 33 34 @Entry 35 @Component 36 struct WebComponent { 37 webviewController: webview.WebviewController = new webview.WebviewController(); 38 // 声明需要注册的对象 39 @State testObj: TestClass = new TestClass(); 40 41 build() { 42 Column() { 43 Button('deleteJavaScriptRegister') 44 .onClick(() => { 45 try { 46 this.webviewController.deleteJavaScriptRegister("testObjName"); 47 } catch (error) { 48 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 49 } 50 }) 51 // Web组件加载本地index.html页面 52 Web({ src: $rawfile('index.html'), controller: this.webviewController}) 53 // 将对象注入到web端 54 .javaScriptProxy({ 55 object: this.testObj, 56 name: "testObjName", 57 methodList: ["test"], 58 controller: this.webviewController, 59 // 可选参数 60 asyncMethodList: [], 61 permission: '{"javascriptProxyPermission":{"urlPermissionList":[{"scheme":"resource","host":"rawfile","port":"","path":""},' + 62 '{"scheme":"e","host":"f","port":"g","path":"h"}],"methodList":[{"methodName":"test","urlPermissionList":' + 63 '[{"scheme":"https","host":"xxx.com","port":"","path":""},{"scheme":"resource","host":"rawfile","port":"","path":""}]},' + 64 '{"methodName":"test11","urlPermissionList":[{"scheme":"q","host":"r","port":"","path":"t"},' + 65 '{"scheme":"u","host":"v","port":"","path":""}]}]}}' 66 }) 67 } 68 } 69 } 70 ``` 71- 应用侧使用[registerJavaScriptProxy()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#registerjavascriptproxy)接口注册示例如下。 72 73 ```ts 74 // xxx.ets 75 import { webview } from '@kit.ArkWeb'; 76 import { BusinessError } from '@kit.BasicServicesKit'; 77 78 class TestClass { 79 constructor() { 80 } 81 82 test(): string { 83 return "ArkUI Web Component"; 84 } 85 86 toString(): void { 87 console.log('Web Component toString'); 88 } 89 } 90 91 @Entry 92 @Component 93 struct Index { 94 webviewController: webview.WebviewController = new webview.WebviewController(); 95 @State testObj: TestClass = new TestClass(); 96 97 build() { 98 Column() { 99 Button('refresh') 100 .onClick(() => { 101 try { 102 this.webviewController.refresh(); 103 } catch (error) { 104 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 105 } 106 }) 107 Button('Register JavaScript To Window') 108 .onClick(() => { 109 try { 110 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"], 111 // 可选参数, asyncMethodList 112 [], 113 // 可选参数, permission 114 '{"javascriptProxyPermission":{"urlPermissionList":[{"scheme":"resource","host":"rawfile","port":"","path":""},' + 115 '{"scheme":"e","host":"f","port":"g","path":"h"}],"methodList":[{"methodName":"test","urlPermissionList":' + 116 '[{"scheme":"https","host":"xxx.com","port":"","path":""},{"scheme":"resource","host":"rawfile","port":"","path":""}]},' + 117 '{"methodName":"test11","urlPermissionList":[{"scheme":"q","host":"r","port":"","path":"t"},' + 118 '{"scheme":"u","host":"v","port":"","path":""}]}]}}' 119 ); 120 } catch (error) { 121 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 122 } 123 }) 124 Button('deleteJavaScriptRegister') 125 .onClick(() => { 126 try { 127 this.webviewController.deleteJavaScriptRegister("testObjName"); 128 } catch (error) { 129 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 130 } 131 }) 132 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 133 } 134 } 135 } 136 ``` 137 138 > **说明:** 139 > 140 > - 使用[registerJavaScriptProxy()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#registerjavascriptproxy)方法注册时,注册后需调用[refresh()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#refresh)方法生效。 141 142- 可选参数permission是一个json字符串,示例如下: 143 ```json 144 { 145 "javascriptProxyPermission": { 146 "urlPermissionList": [ // Object级权限,如果匹配,所有Method都授权 147 { 148 "scheme": "resource", // 精确匹配,不能为空,必填 149 "host": "rawfile", // 精确匹配,不能为空,必填 150 "port": "", // 精确匹配,为空不检查,必填 151 "path": "" // 前缀匹配,为空不检查,必填 152 }, 153 { 154 "scheme": "https", // 精确匹配,不能为空,必填 155 "host": "xxx.com", // 精确匹配,不能为空,必填 156 "port": "8080", // 精确匹配,为空不检查,必填 157 "path": "a/b/c" // 前缀匹配,为空不检查,必填 158 } 159 ], 160 "methodList": [ 161 { 162 "methodName": "test", 163 "urlPermissionList": [ // Method级权限 164 { 165 "scheme": "https", // 精确匹配,不能为空,必填 166 "host": "xxx.com", // 精确匹配,不能为空,必填 167 "port": "", // 精确匹配,为空不检查,必填 168 "path": "" // 前缀匹配,为空不检查,必填 169 }, 170 { 171 "scheme": "resource",// 精确匹配,不能为空,必填 172 "host": "rawfile", // 精确匹配,不能为空,必填 173 "port": "", // 精确匹配,为空不检查,必填 174 "path": "" // 前缀匹配,为空不检查,必填 175 } 176 ] 177 }, 178 { 179 "methodName": "test11", 180 "urlPermissionList": [ // Method级权限 181 { 182 "scheme": "q", // 精确匹配,不能为空,必填 183 "host": "r", // 精确匹配,不能为空,必填 184 "port": "", // 精确匹配,为空不检查,必填 185 "path": "t" // 前缀匹配,为空不检查,必填 186 }, 187 { 188 "scheme": "u", // 精确匹配,不能为空,必填 189 "host": "v", // 精确匹配,不能为空,必填 190 "port": "", // 精确匹配,为空不检查,必填 191 "path": "" // 前缀匹配,为空不检查,必填 192 } 193 ] 194 } 195 ] 196 } 197 } 198 ``` 199 200- index.html前端页面触发应用侧代码。 201 202 ```html 203 <!-- index.html --> 204 <!DOCTYPE html> 205 <html> 206 <body> 207 <button type="button" onclick="callArkTS()">Click Me!</button> 208 <p id="demo"></p> 209 <script> 210 function callArkTS() { 211 let str = testObjName.test(); 212 document.getElementById("demo").innerHTML = str; 213 console.info('ArkTS Hello World! :' + str); 214 } 215 </script> 216 </body> 217 </html> 218 ``` 219## 复杂类型使用方法 220 221### 应用侧和前端页面之间传递Array 222 223 Array可以作为注册对象方法的参数或返回值,在应用侧和前端页面之间传递。 224 ```ts 225 // xxx.ets 226 import { webview } from '@kit.ArkWeb'; 227 import { BusinessError } from '@kit.BasicServicesKit'; 228 229 class TestClass { 230 constructor() { 231 } 232 233 test(): Array<number> { 234 return [1, 2, 3, 4] 235 } 236 237 toString(param: string): void { 238 console.log('Web Component toString' + param); 239 } 240 } 241 242 @Entry 243 @Component 244 struct Index { 245 webviewController: webview.WebviewController = new webview.WebviewController(); 246 @State testObj: TestClass = new TestClass(); 247 248 build() { 249 Column() { 250 Button('refresh') 251 .onClick(() => { 252 try { 253 this.webviewController.refresh(); 254 } catch (error) { 255 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 256 } 257 }) 258 Button('Register JavaScript To Window') 259 .onClick(() => { 260 try { 261 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 262 } catch (error) { 263 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 264 } 265 }) 266 Button('deleteJavaScriptRegister') 267 .onClick(() => { 268 try { 269 this.webviewController.deleteJavaScriptRegister("testObjName"); 270 } catch (error) { 271 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 272 } 273 }) 274 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 275 } 276 } 277 } 278 ``` 279 280 ```html 281 <!-- index.html --> 282 <!DOCTYPE html> 283 <html> 284 <body> 285 <button type="button" onclick="callArkTS()">Click Me!</button> 286 <p id="demo"></p> 287 <script> 288 function callArkTS() { 289 testObjName.toString(testObjName.test()); 290 } 291 </script> 292 </body> 293 </html> 294 ``` 295 296### 非Function等复杂类型使用 297 298 非Function等复杂类型作为注册对象方法的参数或返回值,在应用侧和前端页面之间传递。 299 ```ts 300 // xxx.ets 301 import { webview } from '@kit.ArkWeb'; 302 import { BusinessError } from '@kit.BasicServicesKit'; 303 304 class Student { 305 name: string = ''; 306 age: string = ''; 307 } 308 309 class TestClass { 310 constructor() { 311 } 312 313 // 传递的基础类型name:"jeck", age:"12"。 314 test(): Student { 315 let st: Student = { name: "jeck", age: "12" }; 316 return st; 317 } 318 319 toString(param: ESObject): void { 320 console.log('Web Component toString' + param["name"]); 321 } 322 } 323 324 @Entry 325 @Component 326 struct Index { 327 webviewController: webview.WebviewController = new webview.WebviewController(); 328 @State testObj: TestClass = new TestClass(); 329 330 build() { 331 Column() { 332 Button('refresh') 333 .onClick(() => { 334 try { 335 this.webviewController.refresh(); 336 } catch (error) { 337 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 338 } 339 }) 340 Button('Register JavaScript To Window') 341 .onClick(() => { 342 try { 343 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 344 } catch (error) { 345 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 346 } 347 }) 348 Button('deleteJavaScriptRegister') 349 .onClick(() => { 350 try { 351 this.webviewController.deleteJavaScriptRegister("testObjName"); 352 } catch (error) { 353 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 354 } 355 }) 356 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 357 } 358 } 359 } 360 ``` 361 362 ```html 363 <!-- index.html --> 364 <!DOCTYPE html> 365 <html> 366 <body> 367 <button type="button" onclick="callArkTS()">Click Me!</button> 368 <p id="demo"></p> 369 <script> 370 function callArkTS() { 371 testObjName.toString(testObjName.test()); 372 } 373 </script> 374 </body> 375 </html> 376 ``` 377### 应用侧调用前端页面的Callback 378 379 Callback可以作为注册对象方法的参数或返回值,在应用侧和前端页面之间传递。 380 ```ts 381 // xxx.ets 382 import { webview } from '@kit.ArkWeb'; 383 import { BusinessError } from '@kit.BasicServicesKit'; 384 385 class TestClass { 386 constructor() { 387 } 388 389 test(param: Function): void { 390 param("call callback"); 391 } 392 393 toString(param: String): void { 394 console.log('Web Component toString' + param); 395 } 396 } 397 398 @Entry 399 @Component 400 struct Index { 401 webviewController: webview.WebviewController = new webview.WebviewController(); 402 @State testObj: TestClass = new TestClass(); 403 404 build() { 405 Column() { 406 Button('refresh') 407 .onClick(() => { 408 try { 409 this.webviewController.refresh(); 410 } catch (error) { 411 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 412 } 413 }) 414 Button('Register JavaScript To Window') 415 .onClick(() => { 416 try { 417 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 418 } catch (error) { 419 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 420 } 421 }) 422 Button('deleteJavaScriptRegister') 423 .onClick(() => { 424 try { 425 this.webviewController.deleteJavaScriptRegister("testObjName"); 426 } catch (error) { 427 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 428 } 429 }) 430 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 431 } 432 } 433 } 434 ``` 435 436 ```html 437 <!-- index.html --> 438 <!DOCTYPE html> 439 <html> 440 <body> 441 <button type="button" onclick="callArkTS()">Click Me!</button> 442 <p id="demo"></p> 443 <script> 444 function callArkTS() { 445 testObjName.test(function(param){testObjName.toString(param)}); 446 } 447 </script> 448 </body> 449 </html> 450 ``` 451### 应用侧调用前端页面Object里的Function 452 453 前端页面Object里的Function可以作为注册对象方法的参数或返回值,在应用侧和前端页面之间传递。 454 ```ts 455 // xxx.ets 456 import { webview } from '@kit.ArkWeb'; 457 import { BusinessError } from '@kit.BasicServicesKit'; 458 459 class TestClass { 460 constructor() { 461 } 462 463 test(param: ESObject): void { 464 param.hello("call obj func"); 465 } 466 467 toString(param: string): void { 468 console.log('Web Component toString' + param); 469 } 470 } 471 472 @Entry 473 @Component 474 struct Index { 475 webviewController: webview.WebviewController = new webview.WebviewController(); 476 @State testObj: TestClass = new TestClass(); 477 478 build() { 479 Column() { 480 Button('refresh') 481 .onClick(() => { 482 try { 483 this.webviewController.refresh(); 484 } catch (error) { 485 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 486 } 487 }) 488 Button('Register JavaScript To Window') 489 .onClick(() => { 490 try { 491 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 492 } catch (error) { 493 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 494 } 495 }) 496 Button('deleteJavaScriptRegister') 497 .onClick(() => { 498 try { 499 this.webviewController.deleteJavaScriptRegister("testObjName"); 500 } catch (error) { 501 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 502 } 503 }) 504 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 505 } 506 } 507 } 508 ``` 509 510 ```html 511 <!-- index.html --> 512 <!DOCTYPE html> 513 <html> 514 <body> 515 <button type="button" onclick="callArkTS()">Click Me!</button> 516 <p id="demo"></p> 517 <script> 518 // 写法1 519 class Student { 520 constructor(nameList) { 521 this.methodNameListForJsProxy = nameList; 522 } 523 524 hello(param) { 525 testObjName.toString(param) 526 } 527 } 528 var st = new Student(["hello"]) 529 530 // 写法2 531 //创建一个构造器,构造函数首字母大写 532 function Obj1(){ 533 this.methodNameListForJsProxy=["hello"]; 534 this.hello=function(param){ 535 testObjName.toString(param) 536 }; 537 } 538 //利用构造器,通过new关键字生成对象 539 var st1 = new Obj1(); 540 541 function callArkTS() { 542 testObjName.test(st); 543 testObjName.test(st1); 544 } 545 </script> 546 </body> 547 </html> 548 ``` 549 550### 前端页面调用应用侧Object里的Function 551 552 应用侧Object里的Function可以作为注册对象方法的参数或返回值,在应用侧和前端页面之间传递。 553 ```ts 554 // xxx.ets 555 import { webview } from '@kit.ArkWeb'; 556 import { BusinessError } from '@kit.BasicServicesKit'; 557 558 class ObjOther { 559 methodNameListForJsProxy: string[] 560 561 constructor(list: string[]) { 562 this.methodNameListForJsProxy = list 563 } 564 565 testOther(json: string): void { 566 console.info(json) 567 } 568 } 569 570 class TestClass { 571 ObjReturn: ObjOther 572 573 constructor() { 574 this.ObjReturn = new ObjOther(["testOther"]); 575 } 576 577 test(): ESObject { 578 return this.ObjReturn 579 } 580 581 toString(param: string): void { 582 console.log('Web Component toString' + param); 583 } 584 } 585 586 @Entry 587 @Component 588 struct Index { 589 webviewController: webview.WebviewController = new webview.WebviewController(); 590 @State testObj: TestClass = new TestClass(); 591 592 build() { 593 Column() { 594 Button('refresh') 595 .onClick(() => { 596 try { 597 this.webviewController.refresh(); 598 } catch (error) { 599 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 600 } 601 }) 602 Button('Register JavaScript To Window') 603 .onClick(() => { 604 try { 605 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 606 } catch (error) { 607 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 608 } 609 }) 610 Button('deleteJavaScriptRegister') 611 .onClick(() => { 612 try { 613 this.webviewController.deleteJavaScriptRegister("testObjName"); 614 } catch (error) { 615 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 616 } 617 }) 618 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 619 } 620 } 621 } 622 ``` 623 624 ```html 625 <!-- index.html --> 626 <!DOCTYPE html> 627 <html> 628 <body> 629 <button type="button" onclick="callArkTS()">Click Me!</button> 630 <p id="demo"></p> 631 <script> 632 function callArkTS() { 633 testObjName.test().testOther("call other object func"); 634 } 635 </script> 636 </body> 637 </html> 638 ``` 639 640### Promise场景 641 642 第一种使用方法,在应用侧new Promise,将Promise作为对象方法的参数或返回值,向前端页面传递。 643 ```ts 644 // xxx.ets 645 import { webview } from '@kit.ArkWeb'; 646 import { BusinessError } from '@kit.BasicServicesKit'; 647 648 class TestClass { 649 constructor() { 650 } 651 652 test(): Promise<string> { 653 let p: Promise<string> = new Promise((resolve, reject) => { 654 setTimeout(() => { 655 console.log('执行完成'); 656 reject('fail'); 657 }, 10000); 658 }); 659 return p; 660 } 661 662 toString(param: string): void { 663 console.log(" " + param); 664 } 665 } 666 667 @Entry 668 @Component 669 struct Index { 670 webviewController: webview.WebviewController = new webview.WebviewController(); 671 @State testObj: TestClass = new TestClass(); 672 673 build() { 674 Column() { 675 Button('refresh') 676 .onClick(() => { 677 try { 678 this.webviewController.refresh(); 679 } catch (error) { 680 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 681 } 682 }) 683 Button('Register JavaScript To Window') 684 .onClick(() => { 685 try { 686 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 687 } catch (error) { 688 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 689 } 690 }) 691 Button('deleteJavaScriptRegister') 692 .onClick(() => { 693 try { 694 this.webviewController.deleteJavaScriptRegister("testObjName"); 695 } catch (error) { 696 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 697 } 698 }) 699 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 700 } 701 } 702 } 703 ``` 704 705 ```html 706 <!-- index.html --> 707 <!DOCTYPE html> 708 <html> 709 <body> 710 <button type="button" onclick="callArkTS()">Click Me!</button> 711 <p id="demo"></p> 712 <script> 713 function callArkTS() { 714 testObjName.test().then((param)=>{testObjName.toString(param)}).catch((param)=>{testObjName.toString(param)}) 715 } 716 </script> 717 </body> 718 </html> 719 ``` 720 第二种使用方法,在前端页面new Promise,将Promise作为对象方法的参数或返回值,向应用侧传递。 721 ```ts 722 // xxx.ets 723 import { webview } from '@kit.ArkWeb'; 724 import { BusinessError } from '@kit.BasicServicesKit'; 725 726 class TestClass { 727 constructor() { 728 } 729 730 test(param:Function): void { 731 setTimeout( () => { param("suc") }, 10000) 732 } 733 734 toString(param:string): void { 735 console.log(" " + param); 736 } 737 } 738 739 @Entry 740 @Component 741 struct Index { 742 webviewController: webview.WebviewController = new webview.WebviewController(); 743 @State testObj: TestClass = new TestClass(); 744 745 build() { 746 Column() { 747 Button('refresh') 748 .onClick(() => { 749 try { 750 this.webviewController.refresh(); 751 } catch (error) { 752 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 753 } 754 }) 755 Button('Register JavaScript To Window') 756 .onClick(() => { 757 try { 758 this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 759 } catch (error) { 760 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 761 } 762 }) 763 Button('deleteJavaScriptRegister') 764 .onClick(() => { 765 try { 766 this.webviewController.deleteJavaScriptRegister("testObjName"); 767 } catch (error) { 768 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 769 } 770 }) 771 Web({ src: $rawfile('index.html'), controller: this.webviewController }) 772 } 773 } 774 } 775 ``` 776 777 ```html 778 <!-- index.html --> 779 <!DOCTYPE html> 780 <html> 781 <body> 782 <button type="button" onclick="callArkTS()">Click Me!</button> 783 <p id="demo"></p> 784 <script> 785 function callArkTS() { 786 let funpromise 787 var p = new Promise(function(resolve, reject){funpromise=(param)=>{resolve(param)}}) 788 testObjName.test(funpromise) 789 p.then((param)=>{testObjName.toString(param)}) 790 } 791 </script> 792 </body> 793 </html> 794 ``` 795## 验证通道是否建立成功 796 7971. 打开web调试。 798 799 开启web调试请参考[使用DevTools工具调试前端页面](web-debugging-with-devtools.md)。 800 8012. 举例说明通道是否建立成功。 802 803 使用[复杂类型使用方法](#复杂类型使用方法)中应用侧和前端页面之间传递Array作为示例,调试结果如下图所示: 804 805 