vue2黑马大事件-更换密码
src/views/user/userPwd.vue
创建一个文件
下面展示一些 内联代码片
。
代码语言:javascript
复制<template>
<el-card class="box-card">
<div slot="header" class="clearfix">
<span>重置密码</span>
</div>
<!-- 表单 -->
<el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
<el-form-item label="原密码" prop="old_pwd">
<el-input v-model="pwdForm.old_pwd" type="password"></el-input>
</el-form-item>
<el-form-item label="新密码" prop="new_pwd">
<el-input v-model="pwdForm.new_pwd" type="password"></el-input>
</el-form-item>
<el-form-item label="确认新密码" prop="re_pwd">
<el-input v-model="pwdForm.re_pwd" type="password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary">修改密码</el-button>
<el-button>重置</el-button>
</el-form-item>
</el-form>
</el-card>
</template>
<script>
export default {
name: 'UserPwd',
data () {
return {
// 表单的数据对象
pwdForm: {
old_pwd: '',
new_pwd: '',
re_pwd: ''
},
// 表单的验证规则对象
pwdFormRules: {
old_pwd: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ pattern: /^S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
],
new_pwd: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ pattern: /^S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
],
re_pwd: [
{ required: true, message: '请再次确认新密码', trigger: 'blur' },
{ pattern: /^S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
]
}
}
}
}
</script>
<style lang="less" scoped>
.el-form {
width: 500px;
}
</style>
声明对应路由
代码语言:javascript
复制 {
path: 'user-pwd',
component: () => import('@/views/User/userPwd')
}
userPwd.vue文件下
代码语言:javascript
复制 methods: {
// 更新密码
updatePwdFn () {
// 兜底校验
this.$refs.pwdFormRef.validate(async valid => {
if (valid) {
const { data: res } = await updataPwdAPI(this.pwdForm)
if (res.code !== 0) return this.$message.error('原密码错误')
this.$message.success('更新密码成功')
this.$refs.pwdFormRef.resetFields()
// 重置密码(被动退出)
this.$store.commit('updateToken', '')
this.$store.commit('updateUserInfo', {})
this.$router.push('/login')
} else {
return false
}
})
},
// 重置密码
updatePwdreset () {
this.$refs.pwdFormRef.resetFields()
}
}
效果如下
- 输入旧密码
- 输入新密码
- 确认新密码
- 即可交给后台 然后强制退出,再次登录账号和新密码即可