Vue登录页面
Posted wangyawei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue登录页面相关的知识,希望对你有一定的参考价值。
技术选型:vue+element-ui+axios+webpack
1.页面的基本结构
- <div class="login-wrap">
- <div class="ms-title">登录页面</div>
- <div class="ms-login">
- <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
- <el-form-item prop="username">
- <el-input v-model="ruleForm.username" placeholder="用户名"></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-input>
- </el-form-item>
- <!-- `checked` 为 true 或 false -->
- <el-checkbox v-model="checked">记住密码</el-checkbox>
- <br>
- <br>
- <div class="login-btn">
- <el-button type="primary" @click="submitForm(‘ruleForm‘)">登录</el-button>
- </div>
- </el-form>
- </div>
- </div>
2.用mockjs模拟后端接口
- import Mock from ‘mockjs‘; //es6语法引入mock模块
- export default Mock.mock(‘http://test.advance.ai/login‘, { //输出数据
- ‘username‘: ‘17610603706‘,
- ‘password‘: ‘123456‘
- //还可以自定义其他数据
- });
3.在created生命周期函数里边请求后端数据
- //data和method已经初始化,DOM还没有渲染出来,适合请求数据
- created(){
- //请求模拟的后端地址,返回用户名和密码
- axios.get(‘http://test.advance.ai/login‘)
- .then(res => {
- if(res){
- console.log(res.data.username);
- this.loginName=res.data.username;
- this.loginPassword=res.data.password;
- }
- })
- },
表单验证,手机号要写正则表达式验证
- rules: {
- username: [{
- required: true,
- trigger: ‘blur‘,
- validator: validPhone //手机号校验规则
- }],
- password: [{
- required: true,
- message: ‘请输入密码‘,
- trigger: ‘blur‘
- }]
- }
手机号正则表达式验证,validPhone变量
- //校验手机号
- var validPhone=(rule, value,callback)=>{
- if (!value){
- callback(new Error(‘请输入电话号码‘))
- }else if (!isvalidPhone(value)){
- callback(new Error(‘请输入正确的11位手机号码‘))
- }else {
- callback()
- }
- }
- function isvalidPhone(str) {
- //验证手机号的正则表达式
- const reg = /^1[3|4|5|7|8][0-9]d{8}$/
- return reg.test(str)
- }
4.登录事件,登录之前先验证表单
- submitForm(formName) {
- const self = this;
- self.$refs[formName].validate((valid) => {
- if (valid) {
- //判断复选框是否被勾选 勾选则调用配置cookie方法
- if (self.checked == true) {
- console.log("checked == true");
- //传入账号名,密码,和保存天数3个参数
- self.setCookie(self.ruleForm.username, self.ruleForm.password, 7);
- }else {
- console.log("清空Cookie");
- //清空Cookie
- self.clearCookie();
- }
- if(self.loginName==self.ruleForm.username&&self.loginPassword==self.ruleForm.password){
- console.log("登陆成功");
- alert("登录成功");
- }else{
- alert("登录失败,请重新登录");
- }
- } else {
- console.log(‘error submit!!‘);
- return false;
- }
- });
- },
5.设置和获取cookie
- //设置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];
- }
- }
- }
- },
以上是关于Vue登录页面的主要内容,如果未能解决你的问题,请参考以下文章