最近有一个项目表单中有密码框,但是在浏览器上获取input焦点后就会出现之前记住的账号密码信息,产品要求不需要显示这个下拉框。
翻阅了大量的资料并没有好的解决方案;
比较详细的参考链接:https://blog.csdn.net/weixin_43043994/article/details/97890856
项目是基于vue2+elementui框架下开发的
以下提供个人觉得更好的思路与代码
大致思路:
当输入框有值时input设置为password类型,没有值或者清空值时重新渲染input框为text类型。
详细:调用el-input组件,配置show-password项;
加载前(created方法)先先判断密码框是否有数据,如果有则isShowPwd设置为true,如果没有则设置为false
监听input事件,当值为空时设置input框类型为text,show-password设置为false,同时改变key值,让input框重新渲染然后获取焦点;如果值不为空则show-password设置为true
<el-input
:key="pwdKey"
ref="pwd-input"
size="default"
:type="pwdType"
v-model="value"
placeholder="请输入密码"
@input="pwdInput"
/>
data(){
value: '',
pwdType: 'text',
pwdKey: 0
iptKey:0
},
methods: {
pwdInput(e) {
if(e){
this.pwdType = 'password';
} else {
this.pwdKey ++;
this.pwdType = 'text';
this.$nextTick(() => {
this.$refs['pwd-input'].focus();
});
}
}
}
缺点:当值为空时会重新渲染input,会快速闪一下