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

<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

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script s…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

vue实现点击下载

vue实现点击下载

使用 <a> 标签实现下载 在 Vue 中可以通过动态生成 <a> 标签实现文件下载。适用于已知文件 URL 的场景。 <template> <but…