帮我写一个vue框架的 登录界面
时间: 2023-03-19 10:24:59 浏览: 98
好的,以下是一个基本的Vue登录界面的代码示例:
```
<template>
<div class="login-container">
<form class="login-form" @submit.prevent="login">
<h2>Login</h2>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" v-model="email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" v-model="password" required>
</div>
<button type="submit">Login</button>
<div class="error-message">{{ errorMessage }}</div>
</form>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
email: "",
password: "",
errorMessage: "",
};
},
methods: {
login() {
// Here you can make a request to your API to validate user credentials
// and retrieve the authentication token if they are correct.
// For this example, we will just check if the email and password are valid.
if (this.email === "user@example.com" && this.password === "password") {
// If the credentials are correct, store the authentication token in local storage.
const authToken = "some-auth-token";
localStorage.setItem("authToken", authToken);
// Redirect the user to the dashboard page.
this.$router.push("/dashboard");
} else {
// If the credentials are not correct, display an error message.
this.errorMessage = "Invalid email or password.";
}
},
},
};
</script>
<style>
.login-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-form {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-group {
display: flex;
flex-direction: column;
margin-bottom: 10px;
}
label {
font-weight: bold;
margin-bottom: 5px;
}
input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
width: 100%;
font-size: 16px;
margin-bottom: 10px;
}
button {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
font-size: 16px;
cursor: pointer;
}
.error-message {
color: red;
margin-top: 10px;
}
</style>
```
在这个示例中,我们创建了一个名为“Login”的Vue组件,它包含一个表单,其中包含电子邮件和密码字段,以及一个提交按钮。当用户提交表单时,我们在`login`方法中验证用户的凭据。如果凭据有效,我们将一个名为“authToken”的身份验证令牌存储在本地存储中,并使用Vue路由器将用户重定向到“/dashboard”页面。如果凭证无效,我们将在错误消息`errorMessage`中显示错误消息。
此示例仅用于演示目的,实际上您需要替换`login`方法中
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)