Vue實現(xiàn)阻止瀏覽器記住密碼功能的三種方法
前言
通常瀏覽器會主動識別密碼表單,在你登錄成功之后提示保存密碼 , 密碼保存到瀏覽器的 密碼管理器中 ( 如下是谷歌瀏覽器 )


這種行為是瀏覽器的行為 ,這種操作也是為了方便用戶的使用
現(xiàn)在的一個需求是要阻止這個保存密碼的彈窗提示
實現(xiàn)方法
查找資料發(fā)現(xiàn)的一些方法:
- 使用 autocomplete="off"(現(xiàn)代瀏覽器許多都不支持)
- 使用 autocomplete="new-password"
- 在真正的賬號密碼框之前增加相同 name 的 input 框
- 使用 readonly 屬性,在聚焦時移除該屬性
- 初始化 input 框的 type 屬性為 text,聚焦時修改為 password
- 使用 type="text",手動替換文本框內(nèi)容為星號 “*” 或者 小圓點 “●”
可以看到實現(xiàn)這個需求有許許多多的方法,但是實際上確有各種各樣的問題,下面是我自己測試的幾個方法
方法一
密碼框添加 autocomplete="new-password" 屬性
根 index.html 文件添加 meta 元數(shù)據(jù) (該步驟可以省略)
密碼輸入框
<el-input
prefix-icon="el-icon-lock"
type="password"
name="xxxx"
placeholder="密碼"
v-model="password"
autocomplete="new-password"
>
</el-input>index.html 文件 (該步驟可以省略)
<!DOCTYPE html>
<html lang="">
<head>
.....
<meta name="autocomplete" content="off">
.......
</head>
......
<body>
........
</body>
</html>注意
這種方法可以適配 谷歌瀏覽器 、 Edge 、 IE 瀏覽器
不兼容 火狐瀏覽器
方法二
新添加兩個輸入框 , type 分別設(shè)置為 text 和 password
設(shè)置 display: none 屬性讓其隱藏
具體代碼
<div class="login-location">
<!-- 隱藏的輸入框 -->
<el-input
style="display: none"
type="text"
name="xxxx"
autocomplete="off"
>
</el-input>
<el-input
style="display: none"
type="password"
name="xxxx"
autocomplete="off"
>
</el-input>
<!-- 真實輸入框 -->
<el-input
ref="pass"
prefix-icon="el-icon-lock"
@keyup.enter.native="userLogin()"
v-model="password"
type="password"
placeholder="密碼"
name="xxxx"
>
</el-input>
</div>注意
這種方法可以適配 谷歌瀏覽器 、 Edge 、 IE 瀏覽器
不兼容 火狐瀏覽器
方法三
把密碼框的 type 定義為 text ,這樣瀏覽器就無法正確自動識別 密碼
給輸入框綁定事件 , 每次輸入數(shù)據(jù)觸發(fā)事件, 把 密碼 替換成 圓點 ●
給輸入框右側(cè) 小眼睛手動綁定事件 ,控制密碼的顯示隱藏
具體代碼
<div class="login-location">
<el-input
prefix-icon="el-icon-lock"
v-model="pwdCover"
type="text"
name="pwd"
id="pwd"
placeholder="密碼"
autocomplete="off"
@input="setPassword"
@keyup.enter.native="userLogin()"
>
<i
slot="suffix"
class="el-icon-view"
style="margin-top: 10px; margin-right: 10px; font-size: 18px"
@click="hidePassword"
></i>
</el-input>
</div> // 輸入框輸入事件
setPassword(val) {
if (this.isShowPassword) {
this.password = val;
} else {
let reg = /[0-9a-zA-Z]/g; // 只允許輸入字母和數(shù)字
let nDot = /[^●]/g; // 非圓點字符
let index = -1; // 新輸入的字符位置
let lastChar = void 0; // 新輸入的字符
let realArr = this.password.split(""); // 真實密碼數(shù)組
let coverArr = val.split(""); // 文本框顯示密碼數(shù)組
let coverLen = val.length; // 文本框字符串長度
let realLen = this.password.length; // 真實密碼長度
// 找到新輸入的字符及位置
coverArr.forEach((el, idx) => {
if (nDot.test(el)) {
index = idx;
lastChar = el;
}
});
// 判斷輸入的字符是否符合規(guī)范,不符合的話去掉該字符
if (lastChar && !reg.test(lastChar)) {
coverArr.splice(index, 1);
this.pwdCover = coverArr.join("");
return;
}
if (realLen < coverLen) {
// 新增字符
realArr.splice(index, 0, lastChar);
} else if (coverLen <= realLen && index !== -1) {
// 替換字符(選取一個或多個字符直接替換)
realArr.splice(index, realLen - (coverLen - 1), lastChar);
} else {
// 刪除字符,因為 val 全是 ● ,沒有辦法匹配,不知道是從末尾還是中間刪除的字符,刪除了幾個,不好對 password 處理,所以可以通過光標的位置和 val 的長度來判斷
let pos = document.getElementById("pwd").selectionEnd; // 獲取光標位置
realArr.splice(pos, realLen - coverLen);
}
// 將 pwdCover 替換成 ●
this.pwdCover = val.replace(/\S/g, "●");
this.password = realArr.join("");
}
},
// 點擊右側(cè)小眼睛控制顯示隱藏
hidePassword() {
if (!this.isShowPassword) {
// console.log("顯示");
this.isShowPassword = true;
this.pwdCover = this.password;
} else {
// console.log("隱藏");
this.isShowPassword = false;
this.pwdCover = this.pwdCover.replace(/\S/g, "●");
}
},
// 登錄的邏輯
userLogin() {
if (this.account === "admin" && this.password === "admin") {
this.loading = true;
setTimeout(() => {
this.$router.push({ path: "/main" });
}, 600);
this.$message.success("登錄成功");
}
}注意
這種方法可以適配 谷歌瀏覽器 、 Edge 、 IE 瀏覽器
同時兼容 火狐瀏覽器
目前本地測試暫未發(fā)現(xiàn)問題
到此這篇關(guān)于Vue實現(xiàn)阻止瀏覽器記住密碼功能的三種方法的文章就介紹到這了,更多相關(guān)Vue 阻止瀏覽器記住密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue props傳值失敗 輸出undefined的解決方法
今天小編就為大家分享一篇vue props傳值失敗 輸出undefined的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
VUE3刷新頁面報錯問題解決:Uncaught?SyntaxError:Unexpected?token?&apo
這篇文章主要介紹了VUE3刷新頁面報錯:Uncaught?SyntaxError:?Unexpected?token?‘<‘,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
el-table 表格最大高度max-height的問題解決
在工作中遇到了多個滾動條的情況,是因為el-table的max-height設(shè)置為固定值導(dǎo)致的,本文主要介紹了el-table 表格最大高度max-height的問題解決,具有一定的參考價值,感興趣的可以了解一下2024-07-07
vue3二次封裝element-ui中的table組件的過程詳解
這篇文章主要給大家介紹了vue3二次封裝element-ui中的table組件的過程,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友跟著小編一起來學(xué)習(xí)吧2024-01-01
vue3 el-form-item如何自定義label標簽內(nèi)容
這篇文章主要介紹了vue3 el-form-item如何自定義label標簽內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

