解决方案有3种:
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct LoginDemo1 {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Web({ src: $rawfile('login.html'), controller: this.controller })
.javaScriptAccess(true)
.fileAccess(true)
.domStorageAccess(true)
.geolocationAccess(true)
.width('100%')
.height('100%');
}
.width('100%')
.height('100%');
}
} login.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录界面</title>
</head>
<body>
<h2>欢迎登录</h2>
<form id="loginForm" autocomplete="on">
<label for="username">用户名:</label><br>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label><br>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">登录</button>
</form>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
// 这里登录逻辑
// ....
// 跳转至首页
window.location.href = './index.html';
});
</script>
</body>
</html> index.html:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
h1 {
font-size: 3rem;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html> import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct LoginDemo2 {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
// src须替换为真实url
Web({ src: 'https://***', controller: this.controller })
.javaScriptAccess(true)
.fileAccess(true)
.domStorageAccess(true)
.geolocationAccess(true)
.width('100%')
.height('100%');
}
.width('100%')
.height('100%');
}
} login.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录界面</title>
</head>
<body>
<h2>欢迎登录</h2>
<form id="loginForm" autocomplete="on">
<label for="username">用户名:</label><br>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label><br>
<!-- 密码框type不使用password,改成text,避免触发密码保险箱 -->
<input type="text" id="password" name="password" required><br><br>
<button type="submit">登录</button>
</form>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
// 这里登录逻辑
// ....
// 跳转至首页
window.location.href = './index.html';
});
</script>
</body>
</html>