Vue3 登录页面实现:使用 axios 和 验证码插件
<script>
import axios from 'axios';
import Vcode from 'vue3-puzzle-vcode';
import { ref } from 'vue';
export default {
name: 'loginPage',
components: {
Vcode
},
data() {
return {
username: '',
password: '',
isShow: ref(false),
show: ref(true)
};
},
methods: {
login(e) {
e.preventDefault();
// 登录逻辑
axios.post('http://nn.nncoding.icu:8080/api/login', {
username: this.username,
password: this.password
}).then(result => {
console.log('登录成功');
console.log(result);
// 在登录成功后,可以根据接口返回的数据进行相应的操作,比如跳转到首页等
}).catch(error => {
alert('登录失败,用户名或密码错误');
console.log(error);
console.log(error.response.data.message);
});
},
onClose() {
this.isShow.value = false;
},
onSuccess() {
this.onClose(); // 验证成功,需要手动关闭模态框
}
},
};
</script>
<template>
<div class="login-page">
<div class="login-container">
<h2 class="login-title">登录</h2>
<el-form class="login-form">
<el-form-item label="账号:">
<el-input v-model="username" type="text" style="width: 250px;" placeholder="手机号" required></el-input>
</el-form-item>
<el-form-item label="密码:">
<el-input v-model="password" type="password" style="width:250px" placeholder="密码" show-password required/> </el-form-item>
<el-form-item>
<Vcode v-if="show" :isShow="isShow" :onClose="onClose" :onSuccess="onSuccess" />
<el-button class="login-button" type="primary" @click="login">登录</el-button>
</el-form-item>
</el-form>
<div class="register-link">
<router-link to="/register">
<el-button type="info" style="width: 100%;">注册</el-button>
</router-link>
</div>
</div>
</div>
</template>
<style>
.login-page {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-container {
width: 400px;
padding: 20px;
border: 1px solid #e5e5e5;
border-radius: 5px;
background-color: #fff;
}
.login-title {
text-align: center;
margin-bottom: 20px;
}
.login-form {
margin-bottom: 20px;
}
.login-button {
width: 100%;
}
.register-link {
text-align: center;
}
</style>
<p>这是一个简单的登录页面代码示例,使用 Vue3 和 axios 实现登录功能,并使用 'vue3-puzzle-vcode' 插件进行验证码验证。登录成功后可以根据接口返回的数据进行相应操作,比如跳转到首页等。登录失败则弹出提示框显示错误信息。</p>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pMje 著作权归作者所有。请勿转载和采集!