生成一个用户登录界面的html代码要求:使用Vue自定义组件带插槽形式制作一个用户登录页面的UI布局要有短信登陆和用户名登录两种方式短信登陆:手机号为11位有效数字单击获取验证码后js直接生成六位随机数验证码框为输入的正确随机数点击登陆alert提示登录成功!密码登录:用户名和密码为username和password点击登录通过alert提示登录成功!不用写邮箱和手机的
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
form {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
label {
margin-top: 20px;
font-size: 20px;
}
input[type="text"], input[type="password"], input[type="number"] {
width: 300px;
height: 30px;
font-size: 18px;
margin-top: 10px;
padding: 5px;
border-radius: 5px;
border: none;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
button {
margin-top: 20px;
width: 200px;
height: 40px;
font-size: 20px;
font-weight: bold;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
cursor: pointer;
}
button:hover {
background-color: #0069d9;
}
</style>
</head>
<body>
<div id="app">
<login-form>
<template v-slot:username>
<label>用户名</label>
<input type="text" v-model="username">
</template>
<template v-slot:password>
<label>密码</label>
<input type="password" v-model="password">
</template>
<template v-slot:mobile>
<label>手机号</label>
<input type="number" v-model="mobile">
<label>验证码</label>
<input type="number" v-model="code">
<button @click="getCode" v-show="!isSent">获取验证码</button>
<button v-show="isSent" disabled>{{countDown}}秒后重新获取</button>
</template>
<button @click="login">登录</button>
</login-form>
</div>
<script>
Vue.component('login-form', {
template: `
<form>
<slot name="username"></slot>
<slot name="password"></slot>
<slot name="mobile"></slot>
</form>
`
});
<pre><code> new Vue({
el: '#app',
data: {
username: '',
password: '',
mobile: '',
code: '',
isSent: false,
countDown: 60
},
methods: {
getCode() {
this.isSent = true;
const timer = setInterval(() => {
this.countDown--;
if (this.countDown === 0) {
clearInterval(timer);
this.isSent = false;
this.countDown = 60;
}
}, 1000);
const code = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;
this.code = code;
alert(`验证码为:${code}`);
},
login() {
if (this.mobile) {
if (this.mobile.toString().length !== 11) {
alert('请输入11位有效手机号');
return;
}
if (this.code.toString().length !== 6) {
alert('请输入6位有效验证码');
return;
}
if (this.code.toString() !== this.$data.code.toString()) {
alert('验证码错误');
return;
}
alert('登录成功');
} else {
if (this.username === 'username' && this.password === 'password') {
alert('登录成功');
} else {
alert('用户名或密码错误');
}
}
}
}
});
</script>
</code></pre>
</body>
</html
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ezt2 著作权归作者所有。请勿转载和采集!