vue3使用localStorage实现登录注册功能实例
作者:万能守恒定律
这篇文章主要给大家介绍了关于vue3使用localStorage实现登录注册功能的相关资料, localStorage这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题,需要的朋友可以参考下
也许我们使用过vuex实现过登录注册,但是我们会发现,vuex一但刷新,就会被消除,于是我们就使用网页的存储方式localStorage的方式进行存储。
首先我们要构造一个vue3的项目。目录创建如下,红色区域为需要修改的地方
App.vue如下
<template> <nav> <router-link to="/login">登录</router-link> | <router-link to="/regester">注册</router-link> </nav> <router-view/> </template> <style lang="scss"> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } nav { padding: 30px; a { font-weight: bold; color: #2c3e50; &.router-link-exact-active { color: #42b983; } } } </style>
接着去配置路由,router下面的index.js
import { createRouter, createWebHashHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' import login from '../views/login.vue' const routes = [ { //这里需要将根目录默认为Home,方便实现用户在保持登录 状态下再次登录时直接跳转至主页面 path:"/", redirect:{ name:"HomeView" } }, { path: "/HomeView", name: "HomeView", component: HomeView, }, { path: "/login", name: "login", component:login } ,{ path: "/regester", name: "regester", component: () => import("../views/regester.vue") } ] const router = createRouter({ history: createWebHashHistory(), routes }) // 路由守卫 router.beforeEach((to,from,next)=> { //登录及注册页面可以直接进入,而主页面需要分情况 if(to.path=='/login') { next(); console.log(localStorage.s); } else if(to.path=='/regester') { next(); } else { if(from.path=="/login")//从登录页面可以直接通过登录进入主页面 { next(); } else{ //从/进入,如果登录状态是true,则直接next进入主页面 if(localStorage.s === "true") { next(); console.log(localStorage['s']) } else {//如果登录状态是false,那么跳转至登录页面,需要登录才能进入主页面 next('/login'); console.log("需要登录") } } } }) export default router
接着我们来看登录,login.vue
<template> <div id="background"> <div class="container"> <form action=""> <h1>Login</h1> <div class="form"> <div class="item"> <label>用户名:</label><input type="text" name="username" v-model.trim="name" placeholder="请输入用户名"> <!-- v-model把输入的值传输给name变量 --> <br/> </div> <div class="item"> <label>密码:</label><input type="password" name="password" v-model.trim="password" placeholder="请输入密码"> <br/> </div> <div class="keep"> <input @click="handlesave" id="yes" type="radio" value="0" ><!-- 点击选中 --> <label for="yes">保持登录状态</label> </div> </div> </form> <button type="submit" @click.prevent="handlelogin">登录 </button> <!-- v-on点击按钮触发handlelogin方法 --> <button @click.prevent="handleregister">注册</button> <router-view></router-view> </div> </div> </template> <style scoped> .container{ width: 480px; height: 300px; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background:#00000090; text-align: center; border-radius: 20px; margin-top: 10px; } .container h1{ color: aliceblue; margin-left: 20px; } .item { color: white; margin-left: 15%; margin-top: 35px; font-size: 20px; text-align: left; } .item label{ float:left; width: 5em; margin-right: 1em; text-align: right; } input{ margin-left: -5px; padding: 4px; border: solid 1px #4e5ef3; outline: 0; font: normal 13px/100% Verdana,Tahoma,sans-serif; width: 200px; height: 23px; background:#f1f1f190; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px; } button{ position: relative; height: 33px; width: 100px; background: rgba(35, 19, 252, 0.425); border-radius: 10px; margin-top: 18px; box-shadow: none; color: white; margin-left: 40px; margin-right: 10px; } .keep{ color: white; } .keep input{ width: 15px; height: 15px; margin-top: 7px; margin-left: 10px; margin-right: 10px; } </style> <script> export default { data(){ return{ name:"",//姓名,用v-model绑定监听,将输入的字符串赋值给name变量 password:"",//密码 st:"false",//false为不保存登录 }; }, methods:{ handlelogin:function() { // 判断注册中的信息 if(this.name===localStorage['name'] && this.password===localStorage['password']) { this.$router.replace('/HomeView');//如果输入的名字以及密码正确路由跳转至个人页面 } else if(this.name==='')//名字为空 { alert('用户名不为空'); } else if(this.password==='')//密码为空 { alert('密码不为空'); } else{ alert('账号不存在,请注册后登录');//查无此号 } }, handleregister:function() { this.$router.replace('/regester')//点击注册按钮,跳转至注册页面 }, //点击保持登录状态触发handlesave handlesave:function(){ this.st="true";//修改登录状态为true localStorage.setItem('s',this.st); console.log(localStorage.s); } } }; </script>
然后是注册界面,regester.vue
<template> <div id="background"> <div id="contain"> <h1>Regester</h1> <div class="form"> <label>用户名:</label><input type="text" v-model.trim="name"><br/> </div> <div class="form"> <label>密码:</label><input type="password" v-model.trim="password"><br/> </div> <div class="form"> <label>邮箱:</label><input type="email" v-model.trim="mail"><br/> </div> <div class="form"> <label>手机号:</label><input type="tel" v-model.trim="tel"><br/> </div> <button @click.prevent="handlefinish">提交</button> </div> </div> </template> <style scoped> #contain{ width: 580px; height: 450px; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background:#00000090; text-align: center; border-radius: 20px; } #contain h1{ color: white; } .form{ color: white; margin-left: 20%; margin-top: 60px; font-size: 20px; text-align: left; } label{ float:left; width: 5em; margin-right: 1em; text-align: right; } input,textarea{ margin-left: 10px; padding: 4px; border: solid 1px #4e5ef3; outline: 0; font: normal 13px/100% Verdana,Tahoma,sans-serif; width: 200px; height: 20px; background:#f1f1f190; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px; } input:hover,textarea:hover,input:focus,textarea:focus{border-color:#0d0aa1;} button{ position: relative; height: 33px; width: 150px; background: rgba(35, 19, 252, 0.425); border-radius: 10px; margin-top: 38px; box-shadow: none; color: white; margin-left: 40px; } </style> <script> export default { name:'regester', props: { msg: String }, data(){ return{ name:"", password:"", mail:"", tel:"" }; }, methods:{ //点击完成按钮触发handlefinish handlefinish:function() { if(localStorage['name']===this.name) { alert("用户名已存在");//如果用户名已存在则无法注册 } else if(this.name==='') { alert("用户名不能为空"); } else{//将新用户信息存储到localStorage localStorage.setItem('name',this.name); localStorage.setItem('password',this.password); localStorage.setItem('mail',this.mail); localStorage.setItem('tel',this.tel); localStorage.setItem('s',"false"); alert("注册成功"); this.$router.replace('/login');//完成注册后跳转至登录页面 } } } }; </script>
最后是展示信息的页面,HomeView.vue
<template> <div id="bg"> <div id="container"> <h1>个人信息</h1> <p><span>姓名:</span>{{sname}}</p> <p><span>邮箱:</span>{{smail}}</p> <p><span>手机号:</span>{{stel}}</p> <button @click.prevent="logout">退出</button> </div> </div> </template> <script> export default { name:'HomeView', data(){ return{//获取用户信息到主页 sname:localStorage.getItem('name'), smail:localStorage.getItem('mail'), stel:localStorage.getItem('tel'), isAuth:"",//是否保持登录状态 }; }, methods:{ //当点击退出按钮,将不保存登录状态 logout:function() { this.isAuth="false";//修改登录状态 localStorage.setItem('s',this.isAuth); this.$router.replace('/login');//页面跳转至登录页面 } } } </script> <style scoped> #container{ width: 480px; height: 300px; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background:#00000090; text-align: center; border-radius: 20px; margin-top: 10px; color: white; } p{ font-size: 20px; margin-top: 20px; text-align: left; margin-left: 32%; } button{ position: relative; height: 33px; width: 150px; background: rgba(35, 19, 252, 0.425); border-radius: 10px; margin-top: 10px; box-shadow: none; color: white; margin-left: 10px; } </style>
我们来看效果图:
好啦,其实登录注册的形式有很多种,可以是根据后端,也可以采用node.js中端形式,也可以用vuex临时存储~
总结
到此这篇关于vue3使用localStorage实现登录注册功能的文章就介绍到这了,更多相关vue3 localStorage实现登录注册内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!