




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Vue注冊模塊與登錄狀態(tài)的持久化實現(xiàn)方法詳解目錄整體框架1.前端頁面授權(quán)2.實現(xiàn)注冊頁面3.實現(xiàn)登錄狀態(tài)的持久化優(yōu)化前端
整體框架
1.前端頁面授權(quán)
當(dāng)我們登錄網(wǎng)站的時候,如果沒有登錄,強(qiáng)制讓用戶重定向到登錄界面
在router目錄下的index.js文件下實現(xiàn)。router-index.js
importstorefrom'../store/index'
//把一些額外信息放到一個額外的域里面,meta信息里面存一下是否要授權(quán),如果需要授權(quán)而且沒有登錄,重定向到登錄頁面,重定向到登錄界面。
constroutes=[
path:"/",
name:"home",
redirect:"/pk/",
meta:{
requestAuth:true,
path:"/pk/",
name:"pk_index",
component:PkIndexView,
meta:{
requestAuth:true,
path:"/record/",
name:"record_index",
component:RecordIndexView,
meta:{
requestAuth:true,
path:"/ranklist/",
name:"ranklist_index",
component:RanklistIndexView,
meta:{
requestAuth:true,
path:"/user/bot/",
name:"user_bot_index",
component:UserBotIndexView,
meta:{
requestAuth:true,
path:"/user/account/login",
name:"user_account_login",
component:UserAccountLoginView,
meta:{
requestAuth:false,
path:"/user/account/register",
name:"user_account_register",
component:UserAccountRegisterView,
meta:{
requestAuth:false,
path:"/404/",
name:"404",
component:NotFound,
meta:{
requestAuth:false,
path:"/:catchAll(.*)",
redirect:"/404/",
//to跳轉(zhuǎn)到哪個頁面,from表示從哪個頁面跳轉(zhuǎn)過去
//next的表示將頁面要不要執(zhí)行下一步操作,寫之前首先要記錄每一個未授權(quán)界面
router.beforeEach((to,from,next)={
if(to.meta.requestAuth!store.state.user.is_login){
next({name:"user_account_login"});
}else{
next();
})
最終實現(xiàn)效果:如果處于未登錄狀態(tài),點(diǎn)擊除注冊之外的按鈕頁面會跳轉(zhuǎn)到登錄界面。
2.實現(xiàn)注冊頁面
在view-user-account下的UserAccountRegisterView.vue文件實現(xiàn),實現(xiàn)方式類似于同目錄下的UserAccountLoginView.vue
可以直接把登錄頁面的樣式復(fù)制過來再做修改。
template
ContentField
div
div
form@submit.prevent="register"
div
labelfor="username"用戶名/label
inputv-model="username"type="text"id="username"placeholder="請輸入用戶名"
/div
div
labelfor="password"密碼/label
inputv-model="password"type="password"id="password"placeholder="請輸入密碼"
/div
div
labelfor="confirmedpassword"密碼/label
inputv-model="confirmedpassword"type="password"id="confirmedpassword"placeholder="請再次輸入密碼"
/div
div{{error_message}}/div
buttontype="submit"提交/button
/form
/div
/div
/ContentField
/template
script
importContentFieldfrom'../../../components/ContentField.vue'
import{ref}from'vue'
importrouterfrom'../../../router/index'
import$from'jquery'
exportdefault{
components:{
ContentField
setup(){
letusername=ref('');
letpassword=ref('');
letconfirmedPassword=ref('');
leterror_message=ref('');
constregister=()={
$.ajax({
url:":8080/user/account/register/",
type:"post",
data:{
username:username.value,
password:password.value,
confirmedPassword:confirmedPassword.value,
success(resp){
//成功直接返回登錄界面
if(resp.error_message==="success"){
router.push({name:"user_account_login"});
}else{
error_message.value=resp.error_message;
return{
username,
password,
confirmedPassword,
error_message,
register,
/script
stylescoped
button{
width:100%;
div.error-message{
color:red;
justify-content:center;
/style
實現(xiàn)效果圖:
在測試的時候可以會遇到不輸入密碼也可以注冊成功的bug,在RegisterServiceImpl.java下修改一下就可以了。
3.實現(xiàn)登錄狀態(tài)的持久化
當(dāng)我們的用戶重定向到登陸頁面的時候,我們需要把用戶的token存儲到瀏覽器的localstorage,這樣就可以實現(xiàn)登錄狀態(tài)持久化。
首先修改store目錄下的-user.js文件,在合適的位置添加下列兩行。
localStorage.setItem("jwt_token",resp.token);
localStorage.removeItem("jwt_token");
其次修改view-user-account下的UserAccountLoginView.vue文件
script
importContentFieldfrom'../../../components/ContentField.vue'
import{useStore}from'vuex'
import{ref}from'vue'
importrouterfrom'../../../router/index'
exportdefault{
components:{
ContentField
setup(){
conststore=useStore();
letusername=ref('');
letpassword=ref('');
leterror_message=ref('');
constjwt_token=localStorage.getItem("jwt_token");
if(jwt_token){
mit("updateToken",jwt_token);
store.dispatch("getinfo",{
success(){
router.push({name:"home"});
error(){
}else{
constlogin=()={
error_message.value="";
store.dispatch("login",{
username:username.value,
password:password.value,
success(){
store.dispatch("getinfo",{
success(){
router.push({name:'home'});
console.log(store.state.user);
error(){
error_message.value="用戶名或密碼錯誤";
return{
username,
password,
error_message,
login,
/script
優(yōu)化前端
在實現(xiàn)前端登錄狀態(tài)持久化之后,刷新頁面可能會存在明顯的轉(zhuǎn)換,所以下面對前端頁面進(jìn)行優(yōu)化。
首先在store目錄下的user.js中添加全局變量和下拉函數(shù)。
state:{
id:"",
username:"",
password:"",
photo:"",
token:"",
is_login:false,
pulling_info:true,//是否正在拉取信息
mutations:{
updateUser(state,user){
state.id=user.id;
state.username=user.username;
state.photo=user.photo;
state.is_login=user.is_login;
updateToken(state,token){
state.token=token;
logout(state){
state.id="";
state.username="";
state.photo="";
state.token="";
state.is_login=false;
updatePullingInfo(state,pulling_info){
state.pulling_info=pulling_info;
其次修改UserAccountLoginView.vue
template
ContentFieldv-if="!$store.state.user.pulling_info"
div
div
form@submit.prevent="login"
div
labelfor="username"用戶名/label
inputv-model="username"type="text"id="username"placeholder="請輸入用戶名"
/div
div
labelfor="password"密碼/label
inputv-model="password"type="password"id="password"placeholder="請輸入密碼"
/div
div{{error_message}}/div
buttontype="submit"提交/button
/form
/div
/div
/ContentField
/template
script
setup(){
conststore=useStore();
letusername=ref('');
letpassword=ref('');
leterror
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 3902-2020耕地質(zhì)量地球化學(xué)監(jiān)測技術(shù)規(guī)范
- DB32/T 3804-2020金葉接骨木扦插育苗技術(shù)規(guī)程
- DB32/T 3217-2017公路工程EPS顆粒混合輕質(zhì)材料路堤技術(shù)規(guī)程
- DB31/T 770-2013菊花種苗生產(chǎn)技術(shù)規(guī)程
- DB31/T 680.9-2019城市公共用水定額及其計算方法第9部分:其他經(jīng)營性服務(wù)業(yè)(菜場)
- DB31/T 1166.2-2019司法行政機(jī)關(guān)戒毒診斷評估第2部分:生理脫毒
- DB31/T 1067-2017注水式足部按摩器能效等級及評價方法
- DB31/T 1045-2017家政服務(wù)機(jī)構(gòu)管理要求
- DB31/ 792-2020硅單晶及其硅片單位產(chǎn)品能源消耗限額
- 海南省三亞市2025年八年級《語文》上學(xué)期期末試題與參考答案
- 文創(chuàng)產(chǎn)品設(shè)計課件
- 土地平整工程施工方案與技術(shù)措施
- 基層數(shù)字化治理能力提升的內(nèi)在邏輯與創(chuàng)新路徑
- 《公路橋梁阻尼模數(shù)式伸縮裝置》
- 蒸壓加氣混凝土板檢測原始記錄表(含型式檢驗)
- 南京市房屋租賃合同(試行)(居間服務(wù)版)
- ICU病人鎮(zhèn)靜鎮(zhèn)痛護(hù)理
- 《公路橋涵養(yǎng)護(hù)規(guī)范》(5120-2021)
- 2024專利代理人考試真題及答案
- 2024年高考全國甲卷英語試卷(含答案)
- 網(wǎng)站更新維護(hù)合同模板
評論
0/150
提交評論