ant-design实现隐藏密码,不让浏览器记住密码

方法一 js实现

<template>
    <!-- eslint-disable -->
    <a-input :id="eid" v-bind="$attrs" :value="isShowPassword ? value : passwordHidden" @input="input" @clear="clear"
        @copy="copy" @paste="paste" :allowClear="true">
        <i class="mtico-login-password mtico-login" slot="prefix"></i>
        <a-icon @click="isShowPassword = !isShowPassword" class="isShowPassword" slot="suffix" type="eye"
            v-if="isShowPassword" />
        <a-icon @click="isShowPassword = !isShowPassword" class="isShowPassword" slot="suffix" type="eye-invisible"
            v-else />
    </a-input>
</template>

<script>
/* eslint-disable */
export default {
    name: "PasswordInput",
    props: {
        value: {
            type: String,
            default: ''
        }
    },
    data () {
        return {
            eid: String(Math.random()),
            isShowPassword: true,
            selectionStart: 0,
            selectionEnd: 0,
            clipboardData: ''
        }
    },
    computed: {
        passwordHidden () {
            return this.value.replace(/./g, '•')
        }
    },
    methods: {
        input (e) {
            console.log('input', e)
            // console.log('input e', e)
            // console.log('selectionStart', e.target.selectionStart)
            // console.log('selectionEnd', e.target.selectionEnd)
            // console.log('inputType', e.inputType)
            // console.log('data', e.data)
            let selectionStartCurrent = e.target.selectionStart
            let selectionEndCurrent = e.target.selectionEnd
            // console.log('* input selectionStartCurrent', selectionStartCurrent)
            // console.log('* input selectionStartCurrent', selectionStartCurrent)
            console.log('* input this.selectionStart', this.selectionStart)
            console.log('* input this.selectionEnd', this.selectionEnd)
            if (e.inputType === 'insertText') {
                if (this.selectionStart === this.selectionEnd) {
                    // 单增
                    this.$emit('input', this.value.substring(0, selectionStartCurrent - 1) + e.data + this.value.substring(selectionStartCurrent - 1))
                } else {
                    // 替换
                    this.$emit('input', this.value.substring(0, this.selectionStart) + e.data + this.value.substring(this.selectionEnd))
                }
                this.$nextTick(() => document.getElementById(this.eid).setSelectionRange(selectionStartCurrent, selectionEndCurrent))
            } else if (e.inputType === 'deleteContentBackward') {
                // 单减
                let newVal = e.target.value
                let len = this.value.length - newVal.length
                this.$emit('input', this.value.substring(0, selectionStartCurrent) + this.value.substring(selectionStartCurrent + len))
            } else if (e.inputType === 'insertFromPaste') {
                // 粘贴
                console.log('粘贴', this.clipboardData)
                this.$emit('input', this.value.substring(0, this.selectionStart) + this.clipboardData + this.value.substring(this.selectionEnd))
            } else if (['historyUndo', 'historyRedo'].includes(e.inputType)) {
                // 撤回
                console.log(e.inputType, e.target.value)
                this.$emit('input', e.target.value)
            }

        },
        clear (e) {
            // 清除
            console.log('clear', e)
            this.$emit('input', '')
        },
        copy (e) {
            // 复制
            console.log('copy', e)
        },
        paste (e) {
            // 粘贴
            console.log('paste', e)
            this.clipboardData = e.clipboardData.getData('text')
        },
        selectionchange (e) {
            // console.log('selectionchange', e)
            let activeElement = e.target.activeElement
            if (activeElement.id === this.eid) {
                this.selectionStart = activeElement.selectionStart
                this.selectionEnd = activeElement.selectionEnd
                // console.log('selectionchange this.selectionStart', this.selectionStart)
                // console.log('selectionchange this.selectionEnd', this.selectionEnd)
            }
        }
    },
    mounted () {
        // 监听 selectionchange 事件,在光标位置发生变化时触发
        document.addEventListener('selectionchange', this.selectionchange)
    },
}
</script>

<style lang="less" scoped>
.mtico-login {
    font-size: 16px;
}

.isShowPassword {
    cursor: pointer;
    font-size: 16px;
}
</style>

方法二 CSS实现

/deep/.mask {
    input {
        -webkit-text-security: disc !important;
    }
}

呵呵

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值