使用vue3 El 生成一个登录页面,用post推送后端
时间: 2024-02-13 21:04:49 浏览: 27
好的,下面是一个使用Vue 3和Element UI框架生成一个登录页面的示例代码,并且使用axios库发送POST请求将用户输入的用户名和密码发送给后端服务器:
```html
<template>
<el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="80px" class="login-form">
<el-form-item label="用户名" prop="username">
<el-input v-model="loginForm.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="loginForm.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">登录</el-button>
</el-form-item>
</el-form>
</template>
<script>
import axios from 'axios';
export default {
name: 'LoginPage',
data() {
return {
loginForm: {
username: '',
password: ''
},
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
]
}
};
},
methods: {
submitForm() {
this.$refs.loginForm.validate((valid) => {
if (valid) {
axios.post('/api/login', this.loginForm)
.then((response) => {
console.log(response.data);
// 处理登录成功后的逻辑
})
.catch((error) => {
console.error(error);
// 处理登录失败后的逻辑
});
} else {
console.log('登录表单验证失败');
}
});
}
}
};
</script>
<style scoped>
.login-form {
max-width: 400px;
margin: 0 auto;
margin-top: 100px;
}
</style>
```
在这个示例代码中,我们使用Element UI框架生成了一个登录表单,用户需要输入用户名和密码才能提交表单。当用户点击登录按钮时,我们使用axios库发送一个POST请求到后端服务器的`/api/login`接口,并且将用户输入的用户名和密码作为请求体发送到后端服务器。如果登录成功,我们可以在响应的回调函数中处理成功后的逻辑;如果登录失败,我们可以在catch块中处理失败后的逻辑。需要注意的是,这个示例代码中的请求路径和请求体内容仅仅是示例,请根据你自己的实际情况进行修改。