要在您的项目中实现添加“注销”按钮,点击后跳转到登录注册页面,并同时清空用户登录状态,可以按以下步骤完成:
修改流程概述
- 在头部位置添加注销按钮。
- 清除用户登录状态(LocalStorage 和 Vuex 状态)。
- 跳转到登录注册页面 (
/loginRegister)。
实现步骤
1. 修改 HomeView.vue,添加注销按钮并实现注销功能。
在 el-header 中添加按钮,并绑定点击事件:
<el-header
style="font-family: 楷体; text-align: center; font-weight: bold; background-color: rgb(254, 64, 74); color: white; font-size: 50px; position: relative;"
>
红色精神图书管理系统
<!-- 注销按钮 -->
<el-button
type="danger"
style="position: absolute; top: 10px; right: 20px; font-size: 14px;"
@click="handleLogout"
>
注销
</el-button>
</el-header>
实现注销逻辑
在 script 块中实现清空用户状态和跳转功能:
<script>
export default {
name: "HomeView",
data() {
return {
// 图片列表
carouselImages: [
"./photo/home.png",
"./photo/home1.png",
"./photo/home2.png",
],
};
},
methods: {
// 注销方法
handleLogout() {
// 清空 LocalStorage 中的用户状态
localStorage.removeItem("userInfo"); // 假如用户信息以用户键 `userInfo` 存储
sessionStorage.removeItem("userInfo"); // 如果用户状态保存在 sessionStorage 中
// 如果需要清空 Vuex 中的用户状态
if (this.$store) {
this.$store.commit("clearUserInfo");
}
console.log("用户已注销,登录状态已清空");
// 跳转到登录注册页面
this.$router.push("/loginRegister");
},
},
};
</script>
2. 如果项目中使用了 Vuex,确保 Vuex 存储支持清空用户状态。
在 store/index.js 中添加 clearUserInfo 方法:
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
userInfo: null, // 保存用户信息
},
mutations: {
setUserInfo(state, userInfo) {
state.userInfo = userInfo; // 设置用户信息
},
clearUserInfo(state) {
state.userInfo = null; // 清空用户信息
},
},
actions: {},
modules: {},
});
3. 确保用户登录时正确保存登录状态。
(1) Vuex 存储方法
当用户登录成功后,将其状态存储到 Vuex 中:
例如:登录后调用 setUserInfo 保存用户信息:
this.$store.commit("setUserInfo", {
username: "example_user",
token: "example_token",
});
(2) 使用 LocalStorage 保存状态
当用户登录成功后,可以保存状态到 LocalStorage:
localStorage.setItem(
"userInfo",
JSON.stringify({
username: "example_user",
token: "example_token",
})
);
(可以依据需要选择存储方案)
最终代码示例
HomeView.vue
<template>
<div>
<el-container>
<!-- 侧边栏 -->
<el-aside
style="width: 200px; min-height: 100vh; background-color: rgb(254, 64, 74)"
>
<div
style="height: 60px; display: flex; align-items: center; justify-content: center;"
>
<img src="./photo/GH.jpg" alt="Logo" style="width: 100%; height: auto" />
</div>
<el-menu
class="custom-menu"
style="width: 200px; background-color: #fe8f8f;"
popper-append-to-body
>
<el-row>
<el-col :span="12">
<el-menu
class="custom-menu"
style="width: 200px; background-color: #fe8f8f;"
popper-append-to-body
>
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span>导航栏</span>
</template>
<el-menu-item-group>
<el-menu-item index="2">
<!-- 使用 router-link 跳转 -->
<router-link to="/home#section1">首页</router-link>
</el-menu-item>
<el-menu-item index="4">
<router-link to="/borrow">借阅系统</router-link>
</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-col>
</el-row>
</el-menu>
</el-aside>
<el-container>
<!-- 头部 -->
<el-header
style="font-family: 楷体; text-align: center; font-weight: bold; background-color: rgb(254, 64, 74); color: white; font-size: 50px; position: relative;"
>
红色精神图书管理系统
<!-- 注销按钮 -->
<el-button
type="danger"
style="position: absolute; top: 10px; right: 20px; font-size: 14px;"
@click="handleLogout"
>
注销
</el-button>
</el-header>
<!-- 主要 -->
<el-main>
<el-carousel :interval="5000" arrow="always" height="692px">
<!-- 使用 v-for 循环动态绑定图片 -->
<el-carousel-item
v-for="(image, index) in carouselImages"
:key="index"
>
<div class="carousel-image-container">
<img
:src="require(`${image}`)"
alt="carousel"
class="carousel-image"
/>
</div>
</el-carousel-item>
</el-carousel>
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "HomeView",
data() {
return {
// 图片列表
carouselImages: [
"./photo/home.png",
"./photo/home1.png",
"./photo/home2.png",
],
};
},
methods: {
handleLogout() {
// 清空浏览器存储
localStorage.removeItem("userInfo");
sessionStorage.removeItem("userInfo");
// 清空 Vuex 状态
if (this.$store) {
this.$store.commit("clearUserInfo");
}
console.log("已清空用户登录状态,并跳转至登录页面!");
// 跳转到登录注册页面
this.$router.push("/loginRegister");
},
},
};
</script>
<style scoped>
.el-menu-item:hover {
background-color: yellow !important;
color: red !important; /* 鼠标悬停时文字颜色变为红色 */
}
.carousel-image-container {
width: 100%; /* 与 el-carousel 的宽度一致 */
height: 692px; /* 与 el-carousel 的高度一致 */
overflow: hidden;
text-align: center;
}
.carousel-image {
max-width: 100%;
max-height: 100%;
object-fit: contain; /* 确保图片按比例显示并适应容器 */
}
</style>
效果
-
右上角“注销”按钮:
- 点击后清空 LocalStorage 和 Vuex 中的用户登录状态。
- 跳转到登录注册页面。
-
用户状态管理清理:
- 清空了 LocalStorage 中的用户信息。
- Vuex 中
userInfo 的状态置为 null。
-
登录注册页面展示,用户需重新登录。