当前位置:首页 > VUE

vue实现记住密码

2026-01-15 05:55:51VUE

Vue 实现记住密码功能

在 Vue 中实现记住密码功能通常涉及前端表单处理、本地存储(如 localStorage 或 cookie)以及安全注意事项。以下是具体实现方法:

使用 localStorage 存储用户凭证

  1. 创建登录表单组件 在 Vue 模板中添加复选框绑定 v-model,用于标记是否记住密码:

    <template>
      <form @submit.prevent="handleLogin">
        <input v-model="username" placeholder="用户名" />
        <input v-model="password" type="password" placeholder="密码" />
        <label>
          <input type="checkbox" v-model="rememberMe" /> 记住密码
        </label>
        <button type="submit">登录</button>
      </form>
    </template>
  2. 处理登录逻辑 在提交时根据复选框状态存储数据到 localStorage

    <script>
    export default {
      data() {
        return {
          username: '',
          password: '',
          rememberMe: false
        };
      },
      methods: {
        handleLogin() {
          if (this.rememberMe) {
            localStorage.setItem('username', this.username);
            localStorage.setItem('password', this.password);
          } else {
            localStorage.removeItem('username');
            localStorage.removeItem('password');
          }
          // 调用登录 API
        }
      },
      created() {
        const savedUsername = localStorage.getItem('username');
        const savedPassword = localStorage.getItem('password');
        if (savedUsername && savedPassword) {
          this.username = savedUsername;
          this.password = savedPassword;
          this.rememberMe = true;
        }
      }
    };
    </script>

使用 Cookie 替代 localStorage

如果需要更灵活的过期时间或后端读取,可以使用 js-cookie 库:

  1. 安装依赖:

    npm install js-cookie
  2. 修改存储逻辑:

    import Cookies from 'js-cookie';
    
    // 存储(设置7天过期)
    if (this.rememberMe) {
      Cookies.set('username', this.username, { expires: 7 });
      Cookies.set('password', this.password, { expires: 7 });
    }
    
    // 读取
    const savedUsername = Cookies.get('username');
    const savedPassword = Cookies.get('password');

安全注意事项

  1. 避免明文存储密码 推荐只存储用户名,密码通过后端返回的 token 替代:

    // 登录成功后存储 token
    localStorage.setItem('authToken', response.data.token);
  2. 使用 HTTPS 确保所有传输加密,防止中间人攻击。

  3. 提供显式退出选项 清除所有存储的敏感数据:

    vue实现记住密码

    localStorage.removeItem('authToken');
    Cookies.remove('username');

完整示例(Token 方案)

<template>
  <form @submit.prevent="handleLogin">
    <input v-model="username" placeholder="用户名" />
    <input v-model="password" type="password" placeholder="密码" />
    <label>
      <input type="checkbox" v-model="rememberMe" /> 记住我
    </label>
    <button type="submit">登录</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      rememberMe: false
    };
  },
  created() {
    this.username = localStorage.getItem('username') || '';
    this.rememberMe = !!this.username;
  },
  methods: {
    async handleLogin() {
      const res = await api.login(this.username, this.password);
      if (res.success) {
        if (this.rememberMe) {
          localStorage.setItem('username', this.username);
        } else {
          localStorage.removeItem('username');
        }
        localStorage.setItem('authToken', res.token);
      }
    }
  }
};
</script>

关键点总结

  • 优先使用 token 而非明文密码
  • 提供明显的用户控制选项(如复选框)
  • 敏感数据需设置合理的过期时间
  • 退出登录时彻底清除数据

标签: 密码vue
分享给朋友:

相关文章

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…