【vue】修改密码功能

密码验证与自动填充禁用:前端表单实操
 <el-form :model="form" :rules="rules" ref="password" style="width: 500px; margin: auto">
 	<el-form-item label="当前密码" prop="passWord">
    	<el-input v-model="form.passWord" style="width: 400px"></el-input>
    </el-form-item>
    <el-form-item label="新的密码" prop="newPass">
    	<el-input v-model="form.newPass" style="width: 400px" type="password" autocomplete="new-password"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="confPass">
    	<el-input v-model="form.confPass" style="width: 400px" type="password" autocomplete="new-password"></el-input>
    </el-form-item>
</el-form>

data() {
    var validatePass = (rule, value, callback) => {
      if (this.form.confPass !== this.form.newPass) {
        callback(new Error("两次输入密码不一致!"));
      }
      callback();
    };
    return {
      form: {
        passWord: "",
        newPass: "",
        confPass: "",
      },
      rules: {
        passWord: [
          { required: true, message: "请输入当前密码", trigger: "blur" },
        ],
        newPass: [
          { required: true, message: "请输入新的密码", trigger: "blur" },
          { min: 6, message: "密码长度不低于6位数", trigger: "blur" },
        ],
        confPass: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          { min: 6, message: "密码长度不低于6位数", trigger: "blur" },
          { validator: validatePass, trigger: "blur", required: true },
        ],
      },
    };
  },

注:当设置type="password"时,浏览器回自动填充保存的用户名和密码,此时应禁止自动填充:autocomplete=“new-password” (autocomplete=“off”,这个没试过)

### 在 IntelliJ IDEA 中使用 Vue 实现注册和修改密码功能 #### 准备工作 为了在 IntelliJ IDEA 中创建并配置 Vue 项目来实现注册和修改密码功能,需按照如下操作: 确保已安装最新版本的 IntelliJ IDEA 并设置了 JavaScript 版本为 ECMAScript 6[^2]。 通过插件市场安装 `Vue.js` 插件以获得 `.vue` 文件的支持以及增强 IDE 对于 Vue 开发环境的理解能力[^3]。 #### 创建 Vue 项目结构 启动终端窗口,在目标目录下执行命令初始化一个新的 Vue CLI 应用程序: ```bash npm init vue@latest my-project-name cd my-project-name npm install ``` 接着添加必要的依赖项如 Axios 来处理 HTTP 请求: ```bash npm install axios ``` #### 设计表单组件 构建两个主要视图表单用于展示给用户的交互界面——一个是注册页面,另一个是用来更改现有账户密码的地方。这里给出简化版的例子作为起点。 对于 **RegisterForm.vue** 和 **ChangePasswordForm.vue**, 可能看起来像这样: ```html <!-- RegisterForm.vue --> <template> <el-form :model="form" status-icon ref="ruleFormRef"> <!-- 用户名输入框 --> <el-form-item label="用户名" prop="username"> <el-input v-model="form.username"></el-input> </el-form-item> <!-- 密码输入框 --> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="form.password"></el-input> </el-form-item> <!-- 提交按钮 --> <el-button @click.prevent="submitForm">提交</el-button> </el-form> </template> <script setup lang="ts"> import { reactive, ref } from 'vue' const form = reactive({ username: '', password: '' }) // 表单实例引用 const ruleFormRef = ref() function submitForm() { console.log('Submit!', form) } </script> ``` ```html <!-- ChangePasswordForm.vue --> <template> <el-form :model="form" status-icon ref="changePwdFormRef"> <!-- 当前密码输入框 --> <el-form-item label="当前密码" prop="currentPassword"> <el-input type="password" v-model="form.currentPassword"></el-input> </el-form-item> <!-- 新密码输入框 --> <el-form-item label="新密码" prop="newPassword"> <el-input type="password" v-model="form.newPassword"></el-input> </el-form-item> <!-- 确认新密码输入框 --> <el-form-item label="确认新密码" prop="confirmNewPassword"> <el-input type="password" v-model="form.confirmNewPassword"></el-input> </el-form-item> <!-- 修改按钮 --> <el-button @click.prevent="updatePassword">修改</el-button> </el-form> </template> <script setup lang="ts"> import { reactive, ref } from 'vue' const form = reactive({ currentPassword: '', newPassword: '', confirmNewPassword: '' }) // 表单实例引用 const changePwdFormRef = ref() async function updatePassword() { try { const response = await fetch('/api/change-password', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(form), }) if (response.ok) { alert('密码更新成功') } } catch(error){ console.error('Error updating password:', error); } } </script> ``` 上述代码片段展示了如何利用 Element Plus 的 `<el-form>` 组件及其子元素快速搭建起美观且易于使用的用户登录/注册表单[^1]。 请注意实际应用中还需要考虑安全性措施比如加密传输数据、验证服务器端逻辑等,并且应当遵循 RESTful API 原则设计后端接口以便前端调用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值