前端登录页如何做记住密码
在一般的项目中,我们需要在登录页面点击登录时记住密码,比较方便下次登录时快速登录,这个时候我们需要在点击登录时判断记住密码是否为选中状态,如果是选中状态把用户名和输入框的值存入localStorage,如果未选中则清除存入的值。
//记住密码
if (this.checked) {
localStorage.setItem("username", this.username);
localStorage.setItem("password", this.password);
} else {
localStorage.removeItem("username");
localStorage.removeItem("password");
}
在登录页面的created时获取localStorage的值,如果存在,直接给用户名、密码赋值,然后修改记住密码的按钮状态
this.username = localStorage.getItem("username");
this.password = localStorage.getItem("password");
// 判断是否记住密码
if (typeof this.username == "string") {
this.checked = true;
} else {
this.checked = false;
}