Web页面的显示过程可以被分为多个阶段,包括DNS解析、建立连接、发送请求、接收响应、解析HTML、下载资源等步骤

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Hybrid {
@State message: string = 'Hello World';
webController:webview.WebviewController = new webview.WebviewController()
build() {
Column(){
Button('modify')
.onClick(()=>{
// this.webController.runJavaScript(`myFunction()`)
this.webController.registerJavaScriptProxy(new Inject(),"test",["getLocation"])
//前面注入后需要刷新一下
this.webController.refresh()
})
Web({src:$rawfile("web.html"),controller:this.webController})
}
.height('100%')
.width('100%')
}
}
//工具类函数注入
class Inject{
getLocation(){
AlertDialog.show({message:"Hello"})
}
}

//web.html(注意静态资源只能放在rawfile下)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是标题</h1>
<button onclick="getLocation()">获取位置信息</button>
<script>
function getLocation(){
test.getLocation()
}
</script>
</body>
</html>