当前位置:首页 > VUE

vue实现密码框可见

2026-02-24 10:50:48VUE

实现密码框可见功能

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

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

vue实现密码框可见

<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>

方法二:使用计算属性

vue实现密码框可见

<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)

<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实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式完成,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router…