vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能實(shí)例
也許我們使用過(guò)vuex實(shí)現(xiàn)過(guò)登錄注冊(cè),但是我們會(huì)發(fā)現(xiàn),vuex一但刷新,就會(huì)被消除,于是我們就使用網(wǎng)頁(yè)的存儲(chǔ)方式localStorage的方式進(jìn)行存儲(chǔ)。
首先我們要構(gòu)造一個(gè)vue3的項(xiàng)目。目錄創(chuàng)建如下,紅色區(qū)域?yàn)樾枰薷牡牡胤?/p>

App.vue如下
<template>
<nav>
<router-link to="/login">登錄</router-link> |
<router-link to="/regester">注冊(cè)</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 = [
{
//這里需要將根目錄默認(rèn)為Home,方便實(shí)現(xiàn)用戶在保持登錄 狀態(tài)下再次登錄時(shí)直接跳轉(zhuǎn)至主頁(yè)面
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
})
// 路由守衛(wèi)
router.beforeEach((to,from,next)=>
{
//登錄及注冊(cè)頁(yè)面可以直接進(jìn)入,而主頁(yè)面需要分情況
if(to.path=='/login')
{
next();
console.log(localStorage.s);
}
else if(to.path=='/regester')
{
next();
}
else
{
if(from.path=="/login")//從登錄頁(yè)面可以直接通過(guò)登錄進(jìn)入主頁(yè)面
{
next();
}
else{
//從/進(jìn)入,如果登錄狀態(tài)是true,則直接next進(jìn)入主頁(yè)面
if(localStorage.s === "true")
{
next();
console.log(localStorage['s'])
}
else {//如果登錄狀態(tài)是false,那么跳轉(zhuǎn)至登錄頁(yè)面,需要登錄才能進(jìn)入主頁(yè)面
next('/login');
console.log("需要登錄")
}
}
}
})
export default router接著我們來(lái)看登錄,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="請(qǐng)輸入用戶名">
<!-- v-model把輸入的值傳輸給name變量 -->
<br/>
</div>
<div class="item">
<label>密碼:</label><input type="password" name="password" v-model.trim="password" placeholder="請(qǐng)輸入密碼">
<br/>
</div>
<div class="keep">
<input @click="handlesave" id="yes" type="radio" value="0" ><!-- 點(diǎn)擊選中 -->
<label for="yes">保持登錄狀態(tài)</label>
</div>
</div>
</form>
<button type="submit" @click.prevent="handlelogin">登錄 </button>
<!-- v-on點(diǎn)擊按鈕觸發(fā)handlelogin方法 -->
<button @click.prevent="handleregister">注冊(cè)</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綁定監(jiān)聽(tīng),將輸入的字符串賦值給name變量
password:"",//密碼
st:"false",//false為不保存登錄
};
},
methods:{
handlelogin:function()
{
// 判斷注冊(cè)中的信息
if(this.name===localStorage['name'] && this.password===localStorage['password'])
{
this.$router.replace('/HomeView');//如果輸入的名字以及密碼正確路由跳轉(zhuǎn)至個(gè)人頁(yè)面
}
else if(this.name==='')//名字為空
{
alert('用戶名不為空');
}
else if(this.password==='')//密碼為空
{
alert('密碼不為空');
}
else{
alert('賬號(hào)不存在,請(qǐng)注冊(cè)后登錄');//查無(wú)此號(hào)
}
},
handleregister:function()
{
this.$router.replace('/regester')//點(diǎn)擊注冊(cè)按鈕,跳轉(zhuǎn)至注冊(cè)頁(yè)面
},
//點(diǎn)擊保持登錄狀態(tài)觸發(fā)handlesave
handlesave:function(){
this.st="true";//修改登錄狀態(tài)為true
localStorage.setItem('s',this.st);
console.log(localStorage.s);
}
}
};
</script>然后是注冊(cè)界面,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>手機(jī)號(hào):</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:{
//點(diǎn)擊完成按鈕觸發(fā)handlefinish
handlefinish:function()
{
if(localStorage['name']===this.name)
{
alert("用戶名已存在");//如果用戶名已存在則無(wú)法注冊(cè)
}
else if(this.name==='')
{
alert("用戶名不能為空");
}
else{//將新用戶信息存儲(chǔ)到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("注冊(cè)成功");
this.$router.replace('/login');//完成注冊(cè)后跳轉(zhuǎn)至登錄頁(yè)面
}
}
}
};
</script>最后是展示信息的頁(yè)面,HomeView.vue
<template>
<div id="bg">
<div id="container">
<h1>個(gè)人信息</h1>
<p><span>姓名:</span>{{sname}}</p>
<p><span>郵箱:</span>{{smail}}</p>
<p><span>手機(jī)號(hào):</span>{{stel}}</p>
<button @click.prevent="logout">退出</button>
</div>
</div>
</template>
<script>
export default {
name:'HomeView',
data(){
return{//獲取用戶信息到主頁(yè)
sname:localStorage.getItem('name'),
smail:localStorage.getItem('mail'),
stel:localStorage.getItem('tel'),
isAuth:"",//是否保持登錄狀態(tài)
};
},
methods:{
//當(dāng)點(diǎn)擊退出按鈕,將不保存登錄狀態(tài)
logout:function()
{
this.isAuth="false";//修改登錄狀態(tài)
localStorage.setItem('s',this.isAuth);
this.$router.replace('/login');//頁(yè)面跳轉(zhuǎn)至登錄頁(yè)面
}
}
}
</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>
我們來(lái)看效果圖:


好啦,其實(shí)登錄注冊(cè)的形式有很多種,可以是根據(jù)后端,也可以采用node.js中端形式,也可以用vuex臨時(shí)存儲(chǔ)~
總結(jié)
到此這篇關(guān)于vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能的文章就介紹到這了,更多相關(guān)vue3 localStorage實(shí)現(xiàn)登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue出現(xiàn)did you register the component 
這篇文章主要介紹了Vue出現(xiàn)did you register the component correctly?解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
element el-tooltip動(dòng)態(tài)顯示隱藏(有省略號(hào)顯示,沒(méi)有省略號(hào)不顯示)
本文主要介紹了element el-tooltip動(dòng)態(tài)顯示隱藏,主要實(shí)現(xiàn)有省略號(hào)顯示,沒(méi)有省略號(hào)不顯示,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
如何為Vue3提供一個(gè)可媲美Angular的ioc容器
ue3因其出色的響應(yīng)式系統(tǒng),以及便利的功能特性,完全勝任大型業(yè)務(wù)系統(tǒng)的開(kāi)發(fā),這篇文章主要介紹了如何為Vue3提供一個(gè)可媲美Angular的ioc容器,需要的朋友可以參考下2024-08-08
頁(yè)面內(nèi)錨點(diǎn)定位及跳轉(zhuǎn)方法總結(jié)(推薦)
這篇文章主要介紹了頁(yè)面內(nèi)錨點(diǎn)定位及跳轉(zhuǎn)方法總結(jié),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue項(xiàng)目實(shí)現(xiàn)文件下載進(jìn)度條功能
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)文件下載進(jìn)度條功能,本文通過(guò)具體實(shí)現(xiàn)代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09

