vue实战——登出【详解】
  0bfdPFxSVzID 2023年12月06日 17 0


登出逻辑

  1. 弹窗询问用户是否确定登出
  2. 清除登录状态
    登录状态通常存储在 vuex 和 sessionStorage 中,更彻底的登出还可以把所有本地存储数据都清除掉,如 Cookie 和 localStorage 。
  3. 跳转到登录页面

代码实现

vue实战——登出【详解】_javascript

<div class="loginBox" v-if="isLogin">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <span
                >{{ timeMark }},{{
                  userInfo.nickname || userInfo.account
                }}  </span
              >
              <img
                class="avatar"
                :src="'http://localhost:3000' + userInfo.avatar"
                alt="用户头像"
              />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="userCenter">个人中心</el-dropdown-item>
              <el-dropdown-item divided command="logout">登出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
handleCommand(command) {
      if (command === "userCenter") {
        this.$router.push("/userCenter");
      }
      if (command === "logout") {
        this.logout();
      }
    },
logout() {
      this.$confirm("确定登出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 清空 sessionStorage
          sessionStorage.clear();
          // 重置 vuex,详见 
          this.$store.replaceState(store_State_init);
          // 跳转到登录页面
          this.goto_login();
        })
        .catch(() => {});
    },
goto_login() {
      this.$router.push("/login");
    },


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月06日 0

暂无评论

推荐阅读
0bfdPFxSVzID