vue element input如何让浏览器不保存密码

 更新时间:2024年03月18日 15:31:35   作者:niesiyuan000  
这篇文章主要介绍了vue element input如何让浏览器不保存密码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue element input让浏览器不保存密码

从网上看了很多:

  • 比如  auto-complete="new-password"   
  • 比如  autocomplete="off"

这是真的用不了啊,最后用css可以实现

1、html

<div class="miyaoWrap pr mt10">
  <el-input v-model="reach__vioSecretkey" :class="!reach__seePwd ? 'no-autofill-pwd' : ''" placeholder="请输入密钥" auto-complete="new-password" autocomplete="off" />
  <i class="el-icon-view" @click="seePwd()" />
</div>

2、data

reach__vioSecretkey: '', // 违纪秘钥
reach__seePwd: false,

3、methods

seePwd() {
  this.reach__seePwd = !this.reach__seePwd
},

4、css

.miyaoWrap{
  .no-autofill-pwd {
    ::v-deep .el-input__inner {
      -webkit-text-security: disc !important;
    }
  }
  .el-icon-view {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
  }
}

禁止浏览器记住密码和自动填充 element-ui+vue

vue根据element-ui 自定义密码输入框

防止浏览器 记住密码和自动填充

 <template>
  <div
    class="el-password el-input"
    :class="[size ? 'el-input--' + size : '', { 'is-disabled': disabled }]"
  >
    <input
      class="el-input__inner"
      :placeholder="placeholder"
      ref="input"
      :style="{ paddingRight: padding + 'px' }"
      :disabled="disabled"
      :readonly="readonly"
      @focus="handleFocus"
      @blur="handleBlur"
      @input="handleInput"
      @change="change"
      @compositionstart="handleCompositionStart"
      @compositionend="handleCompositionEnd"
    />
    <div class="tools">
      <i
        v-if="clearable !== false"
        v-show="pwd !== '' && isfocus"
        @mousedown.prevent
        class="el-input__icon el-icon-circle-close el-input__clear"
        @click="clearValue"
      ></i>
      <i
        v-if="showPassword !== false"
        v-show="pwd !== '' || isfocus"
        class="el-input__icon el-icon-view el-input__clear"
        @click="changePasswordShow"
      ></i>
    </div>
  </div>
</template> 

<script>
import emitter from "element-ui/src/mixins/emitter";
//自定义密码输入框//input元素光标操作
class CursorPosition {
  constructor(_inputEl) {
    this._inputEl = _inputEl;
  }
  //获取光标的位置 前,后,以及中间字符
  get() {
    var rangeData = { text: "", start: 0, end: 0 };
    if (this._inputEl.setSelectionRange) {
      // W3C
      this._inputEl.focus();
      rangeData.start = this._inputEl.selectionStart;
      rangeData.end = this._inputEl.selectionEnd;
      rangeData.text =
        rangeData.start != rangeData.end
          ? this._inputEl.value.substring(rangeData.start, rangeData.end)
          : "";
    } else if (document.selection) {
      // IE
      this._inputEl.focus();
      var i,
        oS = document.selection.createRange(),
        oR = document.body.createTextRange();
      oR.moveToElementText(this._inputEl);
      rangeData.text = oS.text;
      rangeData.bookmark = oS.getBookmark();
      for (
        i = 0;
        oR.compareEndPoints("StartToStart", oS) < 0 &&
        oS.moveStart("character", -1) !== 0;
        i++
      ) {
        if (this._inputEl.value.charAt(i) == "\r") {
          i++;
        }
      }
      rangeData.start = i;
      rangeData.end = rangeData.text.length + rangeData.start;
    }
    return rangeData;
  }
  //写入光标的位置
  set(rangeData) {
    var oR;
    if (!rangeData) {
      console.warn("You must get cursor position first.");
    }
    this._inputEl.focus();
    if (this._inputEl.setSelectionRange) {
      //  W3C
      this._inputEl.setSelectionRange(rangeData.start, rangeData.end);
    } else if (this._inputEl.createTextRange) {
      // IE
      oR = this._inputEl.createTextRange();
      if (this._inputEl.value.length === rangeData.start) {
        oR.collapse(false);
        oR.select();
      } else {
        oR.moveToBookmark(rangeData.bookmark);
        oR.select();
      }
    }
  }
}
export default {
  name: "el-password",
  props: {
    value: { default: "" },
    size: { type: String, default: "" },
    placeholder: { type: String, default: "请输入" },
    disabled: { type: [Boolean, String], default: false },
    readonly: { type: [Boolean, String], default: false },
    clearable: { type: [Boolean, String], default: false },
    showPassword: { type: [Boolean, String], default: false },
  },
  data() {
    return {
      symbol: "●", //自定义的密码符号
      pwd: "", //密码明文数据
      padding: 15,
      show: false,
      isfocus: false,
      inputEl: null, //input元素
      isComposing: false, //输入框是否还在输入(记录输入框输入的是虚拟文本还是已确定文本)
    };
  },
  mounted() {
    this.inputEl = this.$refs.input;
    this.pwd = this.value;
    this.inputDataConversion(this.pwd);
  },
  mixins: [emitter],
  watch: {
    value: {
      handler: function (value) {
        if (this.inputEl) {
          this.pwd = value;
          this.inputDataConversion(this.pwd);
        }
      },
    },
    showPassword: {
      handler: function (value) {
        let padding = 15;
        if (value) {
          padding += 18;
        }
        if (this.clearable) {
          padding += 18;
        }
        this.padding = padding;
      },
      immediate: true,
    },
    clearable: {
      handler: function (value) {
        let padding = 15;
        if (value) {
          padding += 18;
        }
        if (this.showPassword) {
          padding += 18;
        }
        this.padding = padding;
      },
      immediate: true,
    },
  },
  methods: {
    select() {
      this.$refs.input.select();
    },
    focus() {
      this.$refs.input.focus();
    },
    blur() {
      this.$refs.input.blur();
    },
    handleFocus(event) {
      this.isfocus = true;
      this.$emit("focus", event);
    },
    handleBlur(event) {
      this.isfocus = false;
      this.$emit("blur", event);
      //校验表单
      this.dispatch("ElFormItem", "el.form.blur", [this.value]);
    },
    change(...args) {
      this.$emit("change", ...args);
    },
    clearValue() {
      this.pwd = "";
      this.inputEl.value = "";
      this.$emit("input", "");
      this.$emit("change", "");
      this.$emit("clear");
      this.$refs.input.focus();
    },
    changePasswordShow() {
      this.show = !this.show;
      this.inputDataConversion(this.pwd);
      this.$refs.input.focus();
    },
    inputDataConversion(value) {
      //输入框里的数据转换,将123转为●●●
      if (!value) {
        this.inputEl.value = "";
        return;
      }
      let data = "";
      for (let i = 0; i < value.length; i++) {
        data += this.symbol;
      }
      //使用元素的dataset属性来存储和访问自定义数据-*属性 (存储转换前数据)
      this.inputEl.dataset.value=  this.pwd;
      this.inputEl.value = this.show ? this.pwd : data;
    },
    pwdSetData(positionIndex, value) {
      //写入原始数据
      let _pwd = value.split(this.symbol).join("");
      if (_pwd) {
        let index = this.pwd.length - (value.length - positionIndex.end);
        this.pwd =
          this.pwd.slice(0, positionIndex.end - _pwd.length) +
          _pwd +
          this.pwd.slice(index);
      } else {
        this.pwd =
          this.pwd.slice(0, positionIndex.end) +
          this.pwd.slice(positionIndex.end + this.pwd.length - value.length);
      }
    },
    handleInput(e) {
      //输入值变化后执行 //撰写期间不应发出输入
      if (this.isComposing) return;
      let cursorPosition = new CursorPosition(this.inputEl);
      let positionIndex = cursorPosition.get();
      let value = e.target.value;
      //整个输入框的值
      if (this.show) {
        this.pwd = value;
      } else {
        this.pwdSetData(positionIndex, value);
        this.inputDataConversion(value);
      }
      cursorPosition.set(positionIndex, this.inputEl);
      this.$emit("input", this.pwd);
    },
    handleCompositionStart() {
      //表示正在写
      this.isComposing = true;
    },
    handleCompositionEnd(e) {
      if (this.isComposing) {
        this.isComposing = false;
        //handleCompositionEnd比handleInput后执行,避免isComposing还为true时handleInput无法执行正确逻辑
        this.handleInput(e);
      }
    },
  },
};
</script>
<style scoped>
.tools {
  position: absolute;
  right: 5px;
  display: flex;
  align-items: center;
  top: 0;
  height: 100%;
  z-index: 1;
}
</style>

引用组件

<template>
  <div>
    <el-form
      style="width: 320px; margin: 50px auto"
      :model="fromData"
      ref="elfrom"
      :rules="rules"
    >
      <el-form-item label="密码" prop="password">
        <el-password
          size="small"
          v-model="fromData.password"
          show-password
          placeholder="请输入密码"
        >
        </el-password>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-password
          size="small"
          v-model="fromData.confirmPassword"
          show-password
          placeholder="请再次输入密码"
        >
        </el-password>
      </el-form-item>
    </el-form>
    <br />
  </div>
</template>

<script>
import elPassword from "./el-password.vue";
export default {
  data() {
    return {
      fromData: {
        password: "",
        confirmPassword: "",
      },
      rules: {
        password: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请输入密码"));
              } else if (
                /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[^a-zA-Z0-9]).{10,20}$/.test(
                  value
                ) === true
              ) {
                callback();
              } else {
                callback(
                  new Error("密码范围在10~20位之间!须包含字母数字特殊符号")
                );
              }
            },
            trigger: "blur",
          },
        ],
        confirmPassword: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请输入确认密码"));
              } else if (value != this.fromData.password) {
                callback(new Error("二次密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  components: {
    elPassword,
  },
};
</script>

效果图

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue项目API接口get请求传递参数方式

    vue项目API接口get请求传递参数方式

    这篇文章主要介绍了vue项目API接口get请求传递参数方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 关于Vue背景图打包之后访问路径错误问题的解决

    关于Vue背景图打包之后访问路径错误问题的解决

    本篇文章主要介绍了关于Vue背景图打包之后访问路径错误问题的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • vue怎样获取当前时间,并且传递给后端(不用注解)

    vue怎样获取当前时间,并且传递给后端(不用注解)

    这篇文章主要介绍了vue怎样获得当前时间,并且传递给后端(不用注解)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • vue实现自定义公共组件及提取公共的方法

    vue实现自定义公共组件及提取公共的方法

    这篇文章主要介绍了vue实现自定义公共组件及提取公共的方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Vue Element前端应用开发之界面语言国际化

    Vue Element前端应用开发之界面语言国际化

    我们开发的系统,一般可以不用考虑语言国际化的问题,大多数系统一般是给本国人使用的,而且直接使用中文开发界面会更加迅速 一些,不过框架最好能够支持国际化的处理,以便在需要的时候,可以花点时间来实现多语言切换的处理,使系统具有更广泛的受众用户。
    2021-05-05
  • 详解Vue中的MVVM原理和实现方法

    详解Vue中的MVVM原理和实现方法

    这篇文章主要介绍了Vue中的MVVM原理和实现方法,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 详解Vue demo实现商品列表的展示

    详解Vue demo实现商品列表的展示

    这篇文章主要介绍了Vue demo实现商品列表的展示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • VUE父组件异步获取数据,子组件接收的值为空的问题

    VUE父组件异步获取数据,子组件接收的值为空的问题

    这篇文章主要介绍了VUE父组件异步获取数据,子组件接收的值为空的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue登录以及权限验证相关的实现

    vue登录以及权限验证相关的实现

    这篇文章主要介绍了vue登录以及权限验证相关的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Vue 中的 computed 和 watch 的区别详解

    Vue 中的 computed 和 watch 的区别详解

    这篇文章主要介绍了Vue中的computed和watch的区别详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09

最新评论