Vue项目登录功能中,保存用户名和密码,下次登录的时候,用户名和密码显示在表单上

界面

<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"
                              @keyup.enter.native="submitForm('ruleForm')">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
               
            </el-form>
        </div>
    </div>
</template>

逻辑

<script>
    export default {
        data: function () {
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                }
            }
        },
				 //页面加载调用获取cookie值
				mounted() {
						this.getCookie();
				},
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('admin/login',this.$qs.stringify(this.ruleForm))
												.then((res) => {
                            console.log(res);
                            if (res.data.code == 0) {
                                localStorage.setItem('ms_username', this.ruleForm.username);
																//保存用户名和密码到cookie
																this.setCookie(this.ruleForm.username,this.ruleForm.password,7);
																//跳转
                                this.$router.push('/');
																this.$message.success(res.data.message);
                            } else {
                                console.log(res.data.message);
                                this.$message.warning(res.data.message);
                            }
                        }).catch((res) => {
                            console.log(res);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
						 //设置cookie
            setCookie(c_name, c_pwd, exdays) {
                var exdate = new Date(); //获取时间
                exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
                //字符串拼接cookie
                window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
                window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
            },
            //读取cookie
            getCookie: function() {
                if (document.cookie.length > 0) {
                    var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
                    for (var i = 0; i < arr.length; i++) {
                        var arr2 = arr[i].split('='); //再次切割
                        //判断查找相对应的值
                        if (arr2[0] == 'userName') {
                            this.ruleForm.username = arr2[1]; //保存到保存数据的地方
                        } else if (arr2[0] == 'userPwd') {
                            this.ruleForm.password = arr2[1];
                        }
                    }
                }
            },
        }
    }
</script>

参考链接https://www.cnblogs.com/nxmin/p/8386031.html

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常感谢您对ChitGPT的信任和使用。关于您的问题,我可以为您提供以下的解答: 首先,在Vue3,可以通过elementUI的ElForm组件进行表单验证。具体步骤如下: 1. 在Vue3项目安装并引入elementUI组件库。 2. 在组件引入ElForm组件,并在html模板使用该组件包含用户名密码和确认密码的输入框及验证规则。 3. 在submit事件调用表单的validate方法进行验证。 以下是具体的示例代码,仅供参考: ``` <template> <el-form :rules="rules" ref="form" label-position="left" label-width="80%"> <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-form-item label="确认密码" prop="confirm"> <el-input type="password" v-model="form.confirm"></el-input> </el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form> </template> <script> import { ElButton, ElForm, ElFormItem, ElInput } from 'element-plus'; export default { components: { ElButton, ElForm, ElFormItem, ElInput }, data() { return { form: { username: '', password: '', confirm: '', }, rules: { username: [ { required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }, ], password: [ { required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }, ], confirm: [ { required: true, message: '请再次输入密码', trigger: 'blur' }, { validator: this.validateConfirm, trigger: 'blur' }, ], }, }; }, methods: { validateConfirm(rule, value, callback) { if (value === '') { callback(new Error('请再次输入密码')); } else if (value !== this.form.password) { callback(new Error('两次输入密码不一致')); } else { callback(); } }, submitForm() { this.$refs.form.validate(valid => { if (valid) { // 表单验证通过,可以处理提交逻辑 } else { // 表单验证失败,需要进行相应处理 } }); }, }, }; </script> ``` 需要注意的是,该示例是基于elementUI 2.x进行开发的,如果您使用的是elementUI 1.x版本,代码会有所不同。此外,具体的验证规则可以根据您的需求进行自定义调整。 希望我的回答能够对您有所帮助,如果您有其他问题,欢迎随咨询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值