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

vue实现密码框显示

<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插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…

vue广播实现

vue广播实现

vue广播实现 在Vue中实现广播功能可以通过事件总线(Event Bus)或Vuex来完成。以下是两种常见的实现方式: 使用事件总线(Event Bus) 事件总线是一种简单的跨组件通信方式,适用…

vue实现销售程序

vue实现销售程序

Vue实现销售程序的基本步骤 搭建Vue项目基础结构 使用Vue CLI或Vite初始化项目,安装必要依赖(如Vue Router、Vuex/Pinia、Element UI/Ant Design V…