请在以上代码中登录成功后预留一个跳转接口,并美化页面
时间: 2024-04-16 18:29:31 浏览: 22
好的,我将在代码中添加一个登录成功后的跳转接口,并对页面进行美化。以下是修改后的代码示例:
```vue
<template>
<div class="login-container">
<h2>Login</h2>
<form @submit.prevent="login" class="login-form">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" v-model="username" />
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" v-model="password" />
</div>
<button type="submit">Submit</button>
</form>
<p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
export default {
setup() {
const username = ref('');
const password = ref('');
const errorMessage = ref('');
const router = useRouter();
const login = () => {
if (username.value === '123' && password.value === '123') {
// 登录成功
errorMessage.value = '';
alert('登录成功!');
// 跳转到其他页面(这里假设跳转到主页)
router.push('/');
} else {
// 登录失败
errorMessage.value = 'Invalid username or password';
}
};
return {
username,
password,
errorMessage,
login,
};
},
};
</script>
<style scoped>
.login-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f2f2f2;
}
.login-form {
display: flex;
flex-direction: column;
align-items: center;
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 10px;
width: 100%;
}
label {
font-weight: bold;
}
input {
width: 100%;
padding: 5px;
}
button {
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.error-message {
color: red;
}
</style>
```
在修改后的代码中,我使用了 `vue-router` 来实现登录成功后的页面跳转。在登录成功后,会弹出登录成功的提示框,并且通过 `router.push('/')` 跳转到其他页面(这里假设跳转到主页)。
同时,我对页面进行了美化,包括居中显示登录表单、添加背景色、设置表单样式等。这只是一个简单的美化示例,你可以根据自己的需求进行更多的样式调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)