当前位置:首页 > VUE

vue实现密码框可见

2026-02-24 10:50:48VUE

实现密码框可见功能

在Vue中实现密码框的可见切换功能,可以通过控制输入框的type属性在passwordtext之间切换。以下是具体实现方法:

方法一:使用v-model和v-if

<template>
  <div>
    <input :type="showPassword ? 'text' : 'password'" v-model="password" placeholder="请输入密码">
    <button @click="showPassword = !showPassword">
      {{ showPassword ? '隐藏' : '显示' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      password: '',
      showPassword: false
    }
  }
}
</script>

方法二:使用计算属性

<template>
  <div>
    <input :type="inputType" v-model="password" placeholder="请输入密码">
    <button @click="togglePassword">
      {{ buttonText }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      password: '',
      showPassword: false
    }
  },
  computed: {
    inputType() {
      return this.showPassword ? 'text' : 'password'
    },
    buttonText() {
      return this.showPassword ? '隐藏' : '显示'
    }
  },
  methods: {
    togglePassword() {
      this.showPassword = !this.showPassword
    }
  }
}
</script>

方法三:使用图标代替按钮

<template>
  <div class="password-input">
    <input :type="showPassword ? 'text' : 'password'" v-model="password" placeholder="请输入密码">
    <span class="toggle-icon" @click="showPassword = !showPassword">
      <i :class="showPassword ? 'el-icon-view' : 'el-icon-view-off'"></i>
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      password: '',
      showPassword: false
    }
  }
}
</script>

<style>
.password-input {
  position: relative;
  display: inline-block;
}
.toggle-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
}
</style>

方法四:使用第三方UI库(如Element UI)

vue实现密码框可见

<template>
  <el-input
    v-model="password"
    placeholder="请输入密码"
    :type="showPassword ? 'text' : 'password'"
  >
    <template #append>
      <el-button
        :icon="showPassword ? 'el-icon-view' : 'el-icon-view-off'"
        @click="showPassword = !showPassword"
      />
    </template>
  </el-input>
</template>

<script>
export default {
  data() {
    return {
      password: '',
      showPassword: false
    }
  }
}
</script>

以上方法都可以实现密码框的可见切换功能,可以根据项目需求选择合适的方式。使用第三方UI库可以快速实现更美观的效果,而原生实现则更加灵活。

标签: 密码vue
分享给朋友:

相关文章

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…

vue实现滚屏

vue实现滚屏

实现滚屏的基本方法 在Vue中实现滚屏效果可以通过多种方式完成,常见的有原生JavaScript滚动方法、第三方库或CSS动画。以下是几种常见实现方式: 使用window.scrollTo或Elem…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…

vue实现评价

vue实现评价

Vue实现评价功能 数据绑定与双向绑定 Vue的核心特性是数据绑定和双向绑定,通过v-model指令可以轻松实现表单输入与应用状态之间的双向绑定。在评价功能中,可以使用v-model绑定评论文本和评分…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…