当前位置:首页 > 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>

使用计算属性优化

计算属性可以简化模板中的逻辑判断:

<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 库,可以直接使用其提供的密码输入组件:

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实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Router 实…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…