input输入框设置 type=‘pasdword‘ 禁止通过浏览器开发者工具修改 type类

页面上通过绑定ref属性来获取DOM元素

<FormItem label="密码:" prop="userPassword">
          <Input
            :type="passwordInputType"
            v-model="formData.userPassword"
            placeholder="请输入密码"
            ref="passwordInput"
          />
        </FormItem>

再data数据中默认给 passwordInputType:"password"

  data() {
    return {
      passwordInputType: "password"
    };
  },

然后自定义一个方法 checkInputType 这个方法可以直接获取到用户再控制台输入来改变type类型,这里如果直接更改这个类型默认还是 密码  password  禁止它更改类型密码明文显示

    checkInputType(e) {
      const inputElement = this.$refs.passwordInput.$el.querySelector('input');
      const observer = new MutationObserver((mutationsList) => {
        for (let mutation of mutationsList) {
          if (mutation.type === 'attributes' && mutation.attributeName === 'type') {
            console.log('Input type changed!');
            // 如果检测到 type 被修改恢复为 password
            if (inputElement.type !== 'password') {
              inputElement.type = 'password';
            }
          }
        }
      });
      observer.observe(inputElement, { attributes: true });
    },

最后一定要再mounted方法里面执行

  mounted() {
    this.checkInputType()
  },

如有更好的解决办法还请大家相互交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小生不才S

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值