Vue登录页实现记住密码的操作

html部分

<el-form-item prop="checked" class="login-item">
  <el-checkbox class="area" v-model="loginForm.checked">记住密码</el-checkbox>
</el-form-item>

data部分

      loginForm: {
        username: "",
        password: "",
        checked: true,
      },

methods部分及mounted部分

  mounted() {
    this.getCookie();
  },
  methods: {
    setCookie(c_name, c_pwd, exdays){
      let exdate = new Date();
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
      window.document.cookie = "username" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie = "password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
    },
    getCookie(){
      if(document.cookie.length > 0){
        let arr = document.cookie.split("; ");//cookie存储格式问题,;后面有一个空格
        for(let i = 0; i < arr.length; i ++){
          let arr2 = arr[i].split("=");
          if(arr2[0] == "username"){
            this.loginForm.username = arr2[1];
          }
          else if(arr2[0] == "password"){
            this.loginForm.password = arr2[1];
          }
        }
      }
    },
    clearCookie(){
      this.setCookie("","",-1);
    },
    submitForm(loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          const self = this;
          if(self.loginForm.checked == true){
            self.setCookie(self.loginForm.username, self.loginForm.password, 7);
          }
          else{
            self.clearCookie();
          }
          //以下为登录逻辑,与记住密码的操作无关。
          let userinfo = this.loginForm;
          login(userinfo).then((res) => {
            let userList = res.user;
            setToken("Token", userList.token);
            this.$router.push({ path: "/" });
            this.$store.dispatch("initLeftMenu"); //设置左边菜单始终为展开状态
          });
        }
      });
    },
  },
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容