当前位置:首页 > VUE

vue实现密码框显示

2026-02-23 19:18:30VUE

Vue 实现密码框显示与隐藏

在 Vue 中实现密码框的显示与隐藏功能,可以通过切换输入框的 type 属性(passwordtext)来实现。以下是几种常见实现方式:

使用 v-model 和 v-if/v-show

通过绑定 type 属性,配合按钮切换密码显示状态:

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

添加图标样式

结合 Font Awesome 或其他图标库,提升用户体验:

<template>
  <div class="password-container">
    <input :type="inputType" v-model="password" placeholder="输入密码">
    <span class="toggle-icon" @click="togglePassword">
      <i :class="['fa', showPassword ? 'fa-eye-slash' : 'fa-eye']"></i>
    </span>
  </div>
</template>

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

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

使用第三方组件库

如果项目中使用 Element UI 或 Vuetify 等 UI 库,可以直接使用其提供的密码输入组件:

vue实现密码框显示

Element UI 示例:

<template>
  <el-input
    v-model="password"
    placeholder="请输入密码"
    show-password
  ></el-input>
</template>

Vuetify 示例:

<template>
  <v-text-field
    v-model="password"
    :append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'"
    :type="showPassword ? 'text' : 'password'"
    @click:append="showPassword = !showPassword"
  ></v-text-field>
</template>

以上方法均可实现密码显示/隐藏功能,开发者可根据项目需求选择适合的实现方式。

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

相关文章

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…