当前位置: 首页 > news >正文

VUE登录(含验证码)、注册页面开发

一、登录Login.vue

在views文件夹内新建Login.vue。新建完成后首先在<template></template>里添加<div> </div>。因为:

Vue 2(严格必须):Vue 2 的模板要求必须只有一个根元素。如果你不包这个

,直接写两个并排的标签,浏览器会直接报错 Component template should contain exactly one root element。所以那时必须用
或其他标签包起来。

新建页面后,首先保证这个页面是可以被访问的,so新建完页面后首先去router文件夹下的index.js文件里写一个新的路由:

constroutes=[{path:'/login',name:'Login',component:()=>import('../views/Login.vue')}]


盒子(div)布局+盒子+flek布局搞定


可以看到图片那侧角是直角,原因是图片把圆角border-radius: 5px遮盖了,解决方法为:添加
overflow: hidde

表单贴边怎么办?1.设置垂直居中(flex布局)在最外面的div盒子模型中添加display: flex;align-items: center,即<div style="flex: 1; display: flex;align-items: center">
怎么让表单左右距离相等?组件水平方向居中:justify-content: center
!验证码怎么弄
有一个开源的库,代码在二、验证ValidCode.vue这一节

<template><divstyle="height:100vh;width:100%;overflow:hidden;display:flex;align-items:center;justify-content:center;background-color:#41a1ff"><divstyle="display:flex;height:60vh;width:60%;background-color:white;border-radius:5px;overflow:hidden"><divstyle="flex:1"><imgsrc="@/assets/img/login.png"alt=""style="width:96%"></div><divstyle="flex:1;display:flex;align-items:center;justify-content:center"><el-form:model="user"style="width:80%"><divstyle="font-size:25px;font-weight:bold;text-align:center;margin-bottom:20px">欢迎登录后台管理系统</div><el-form-itemprop="username"><el-inputprefix-icon="el-icon-user"size="large"placeholder="请输入账号"v-model="user.username"></el-input></el-form-item><el-form-itemprop="password"><el-inputprefix-icon="el-icon-lock"size="large"show-passwordplaceholder="请输入密码"v-model="user.password"></el-input></el-form-item><el-form-itemprop="validCode"><divstyle="display:flex"><el-inputprefix-icon="el-icon-"size="large"style="flex:1"v-model="user.code"></el-input><divstyle="flex:1;justify-content:center;height:40px;width:40px;"><valid-code@update:value="getCode"/><!--验证码--></div></div></el-form-item><el-form-item><el-buttontype="primary"style="width:100%"size="large"@click="login">登 录</el-button></el-form-item><divstyle="display:flex"><divstyle="flex:1">还没有账号?请<spanstyle="color:blue;cursor:pointer">注册</span></div><divstyle="flex:1;text-align:right;cursor:pointer;color:blue">忘记密码</div></div></el-form></div></div></div></template><script>importValidCodefrom"@/components/ValidCode";//验证码exportdefault{name:"Login",components:{ValidCode//验证码},data(){return{code:'',// 验证码组件传递过来的codeuser:{code:'',// 表单里用户输入的code验证码username:'',password:''}}},created(){},methods:{getCode(code){console.log(code)this.code=code// login页面的code值=validCode页面传过来的值 ,this表示return里的内容},login(){}}}</script><stylescoped></style>

到以上代码这一步,下一步写后端连接数据库

二、验证ValidCode.vue

在src文件夹下新建一个文件夹components,再新建一个ValidCode.vue 文件,文件内容为:

<template><divclass="ValidCode disabled-select"style="width:100%;height:100%"@click="refreshCode"><spanv-for="(item,index) in codeList":key="index":style="getStyle(item)">{{item.code}}</span><!-- 注意in不能贴着括号--></div></template><script>exportdefault{name:"validCode",data(){return{length:4,codeList:[]}},mounted(){this.createCode()},methods:{refreshCode(){this.createCode()},createCode(){letlen=this.length,codeList=[],chars='ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstwxyz0123456789',charslen=chars.length//生成for(leti=0;i<len;i++){letrgb=[Math.round(Math.random()*220),Math.round(Math.random()*240),Math.round((Math.random())*200)]codeList.push({code:chars.charAt(Math.floor(Math.random()*charslen)),color:`rgb(${rgb})`,padding:`${[Math.floor(Math.random()*10)]}px`,transform:`rotate(${Math.floor(Math.random()*90)-Math.floor(Math.random()*90)}deg)`})}//指向this.codeList=codeList//将当前数据派发出去this.$emit('update:calue',codeList.map(item=>item.code).join(''))},getStyle(data){return`color:${data.color}; font-size:${data.fontSize}; padding:${data.padding}; transform:${data.transform}`}}}</script><style>.ValiCode{display:flex;justify-content:center;align-items:center;cursor:pointer;}.Valicode span{display:inline-block;font-size:18px;}</style>

三、注册Register.vue

http://www.jsqmd.com/news/1085539/

相关文章:

  • 电容串联与并联实战:平衡电阻如何影响电路性能与可靠性
  • HttpOnly属性详解:为何document.cookie会失效及安全取舍
  • MicroPython mpy 文件:从编译到部署的兼容性实战指南
  • 2026 会议纪要软件哪个好?免费额度够用不踩雷我只留这一款
  • Arduino I2C总线故障排查与多设备协同通讯实战
  • Qt串口编程实战:规避QSerialPort多线程陷阱与waitForReadyRead失效分析
  • STM32CubeMX实战:FMC驱动SDRAM从零到读写验证
  • Swin-Transformer Block核心机制解析:从窗口注意力到相对位置编码
  • [智能体-576]:豆包、Coze、OpenClaw、Hermes 四大智能体完整异同对比
  • 解决方案:如何轻松解决多语言应用乱码问题
  • 【C#】C#驱动Bartender模板:实现标签打印与图片/PDF文件生成一体化方案
  • 如何在电脑上畅玩Switch游戏:yuzu模拟器终极指南
  • DroidCam OBS插件实战指南:将手机摄像头转化为专业直播源
  • Vibe Coding 火了一年,终于现出原形:能跑≠能用
  • Java代码审计入门:从Hello-Java-Sec靶场到SQL注入实战
  • 光学像差详解:从原理到工业视觉应用
  • 终极指南:如何用SketchUp STL插件无缝连接3D设计与打印
  • 【VxWorks实战】从零构建DKM:环境搭建与Hello World
  • 实战指南:CANoe VLAN配置全解析——从硬件驱动到仿真节点的精细化设置
  • 探索ucore操作系统内核:清华大学OS实验环境搭建深度解析
  • 加密流量监控实战:解密MITM、元数据分析与合规成本平衡
  • 抖音直播数据抓取实战手册:5分钟搭建实时弹幕监控系统
  • PortSwigger SQL注入LAB12
  • 5分钟掌握芋道源码框架:企业级开发的完整解决方案
  • VMPDump:攻克VMProtect混淆的逆向工程突破者
  • 从概念到实践:深入解析DFT三大支柱SCAN、BIST与ATPG
  • openEuler命令行实战:从零到精通的系统管理指南
  • 终极流媒体下载方案:N_m3u8DL-RE如何让复杂视频获取变得简单高效
  • 3分钟学会用Buzz离线转录多语言音频:英语、中文、日语谁更准?
  • 终极魔兽世界宏编辑器:GSE-Advanced-Macro-Compiler完整指南