专注app软件定制开发Vue项目保持用户登录状态(localStorage + vuex 刷新页面后状态依然保持)

        在开发中,专注app软件定制开发实现用户的登陆注册功专注app软件定制开发能时常常会有一个问题,专注app软件定制开发那就是我们设置的登录状态,专注app软件定制开发在浏览器页面刷新后就消失了,这其实只是因为我们没有保存用户状态。

这里小马演示使用的是 localStorage + vuex 方法(其他诸如 、cookie 等用法相同,只是功能有所区别)。 


一、实现效果

实现功能:用户登录成功后,刷新浏览器页面或者关闭浏览器再次打开网页后,登录状态依然保持,直到用户点击登出。

二、实现步骤及涉及要点

1. 首先在 vuex 中的 state 属性中添加一个空对象 userInfo{ } 用于保存用户登录后的状态;

涉及要点:

  • state 属性(状态)用于添加多个组件共享的变量,作用类似于 vue 中的 data;

2. 在登录页面中,判断登录成功后创建对象 userInfo{ },并添加描述登录状态的各属性,然后将该对象分别存入 localStorage 和 vuex; 

涉及要点:

  • localStorage 属性允许访问 Document 源的 Storage 对象,存储的数据保存在浏览器会话中;
  • 与 sessionStorage 的唯一区别就是 localStorage 属于永久性存储,除非我们手动清除,而 sessionStorage 属于临时存储,浏览器关闭后便会被清空。
    • 存:localStorage.setItem('myCat', 'Tom');
    • 取:var cat = localStorage.getItem("myCat");
    • 删:localStorage.removeItem("myCat"); 或 localStorage.clear("myCat");
  • JSON.stringify() 系列化对象,将返回的对象类型转为字符串类型;
  • this.$store.state,取 vuex 中 state 中的属性,如:
    • this.$store.state.userInfo = userInfo //取出 vuex 中的 userInfo 并赋值为新的 userInfo

3. 在挂载阶段,判断登录状态 userInfo;设置相关属性之后,就可以正常保存登录状态了。

因为 localStorage 为永久保存,所以即使关闭浏览器再次打开网页登录状态依然存在,除非手动清除 localStorage 数据;

4. 设置登出,清除 localStorage 中的数据;

5. 实现功能。

三、涉及代码

vuex(store/index.js)

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. export default new Vuex.Store({
  5. state: {
  6. userInfo:{}
  7. },
  8. mutations: {
  9. },
  10. actions: {
  11. },
  12. modules: {
  13. }
  14. })

设置登录的页面(部分代码,无法复制即用,仅作参考)

登录方法

  1. //登录方法
  2. login() {
  3. //验证码的验证
  4. var randStr = this.rand.toString().replace(/,/g, ""); //随机生成的验证码为数组形式,此处将其转为字符串并去掉中间相隔的逗号
  5. var codeStr = this.code; //用户输入的验证码
  6. if (randStr.toLowerCase() == codeStr.toLowerCase()) { //比较用户输入的与随机生成的验证码,不区分大小写
  7. //获取登录接口
  8. axios.post("user/login", {
  9. name: this.name,
  10. password: this.password,
  11. administrator: this.usertyp
  12. }).then(result => {
  13. console.log(result.data);
  14. const code = result.data.code;
  15. this.token = code;
  16. if (this.token == 1003) {
  17. this.$message.error('用户名或密码未输入!');
  18. } else if (this.token == 1001) {
  19. this.$message.error('登录失败,请检查用户名或者密码是否正确。');
  20. } else if (this.token == 1005) {
  21. this.$message.error('您不是管理员,无管理员登录权限!');
  22. } else if (this.token == 200) {
  23. if (this.usertyp == "2") { //管理员登录
  24. this.$message.success('登录成功!');
  25. this.dialogFormVisible = false; //登录成功后登录插槽关闭
  26. this.loginReg = false;//隐藏登录注册按钮,显示欢迎信息
  27. this.manage = true;//显示管理员登录信息
  28. let userInfo = {
  29. isLogin: true,
  30. manage: true,
  31. name: this.name
  32. };
  33. localStorage.setItem("userInfo", JSON.stringify(userInfo));
  34. this.$store.state.userInfo = userInfo
  35. console.log('this.$store.state.userInfo', this.$store.state.userInfo)
  36. setTimeout(() => { //此处必须使用vue函数,否则this无法访vue实例
  37. this.$message(`欢迎您,管理员 ${this.name}!`)
  38. }, 2000);
  39. console.log(this.usertyp)
  40. } else if (this.usertyp == "") { //普通用户
  41. this.$message.success('登录成功!');
  42. this.dialogFormVisible = false; //登录成功后插槽关闭
  43. this.loginReg = false;//隐藏登录注册按钮,显示欢迎信息
  44. this.user = true; //显示普通用户登录信息
  45. let userInfo = {
  46. isLogin: true,
  47. manage: false,
  48. name: this.name
  49. }
  50. localStorage.setItem("userInfo", JSON.stringify(userInfo));
  51. this.$store.state.userInfo = userInfo
  52. setTimeout(() => { //此处必须使用vue函数,否则this无法访vue实例
  53. this.$message(`欢迎您,尊贵的晋之魂用户 ${this.name}!`)
  54. }, 2000);
  55. console.log(this.usertyp)
  56. }
  57. this.Cookie.set("UserName", this.name); //将用户名存到cookie
  58. console.log('登录状态为:' + this.token);
  59. }
  60. })
  61. } else {
  62. this.$message.error('请输入正确的验证码');
  63. }
  64. },

退出登录方法

  1. //退出登录
  2. logout() {
  3. this.Cookie.remove("UserName");
  4. this.loginReg = true;
  5. this.manage = false;
  6. this.user = false;
  7. this.log_out = false;
  8. localStorage.clear();
  9. setTimeout(() => {
  10. this.$router.push({
  11. path: '/'
  12. }, () => {
  13. }, () => {
  14. });//退出登录后2秒后跳转至首页
  15. }, 2000)
  16. //加()=>{},()=>{} 可解决路由重复后台报错问题
  17. },

挂载阶段判断登录状态

  1. mounted() {
  2. // 判断登录状态
  3. let userInfo = JSON.parse(localStorage.getItem('userInfo'));
  4. if (null === userInfo) return;
  5. console.log('userInfo', userInfo.isLogin);
  6. if (userInfo.isLogin) {
  7. this.dialogFormVisible = false; //登录成功后插槽关闭
  8. this.loginReg = false;//隐藏登录注册按钮,显示欢迎信息
  9. this.name = userInfo.name;
  10. if (userInfo.manage) {
  11. this.manage = true;//显示管理员登录信息
  12. } else {
  13. this.user = true;//显示普通用户登录信息
  14. }
  15. }
  16. }

提示:小马使用的是 vue + Element UI,使用其他技术代码可能不同,但思路是不变的。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发