版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Vue注冊(cè)模塊與登錄狀態(tài)的持久化實(shí)現(xiàn)方法詳解目錄整體框架1.前端頁(yè)面授權(quán)2.實(shí)現(xiàn)注冊(cè)頁(yè)面3.實(shí)現(xiàn)登錄狀態(tài)的持久化優(yōu)化前端
整體框架
1.前端頁(yè)面授權(quán)
當(dāng)我們登錄網(wǎng)站的時(shí)候,如果沒有登錄,強(qiáng)制讓用戶重定向到登錄界面
在router目錄下的index.js文件下實(shí)現(xiàn)。router-index.js
importstorefrom'../store/index'
//把一些額外信息放到一個(gè)額外的域里面,meta信息里面存一下是否要授權(quán),如果需要授權(quán)而且沒有登錄,重定向到登錄頁(yè)面,重定向到登錄界面。
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)到哪個(gè)頁(yè)面,from表示從哪個(gè)頁(yè)面跳轉(zhuǎn)過(guò)去
//next的表示將頁(yè)面要不要執(zhí)行下一步操作,寫之前首先要記錄每一個(gè)未授權(quán)界面
router.beforeEach((to,from,next)={
if(to.meta.requestAuth!store.state.user.is_login){
next({name:"user_account_login"});
}else{
next();
})
最終實(shí)現(xiàn)效果:如果處于未登錄狀態(tài),點(diǎn)擊除注冊(cè)之外的按鈕頁(yè)面會(huì)跳轉(zhuǎn)到登錄界面。
2.實(shí)現(xiàn)注冊(cè)頁(yè)面
在view-user-account下的UserAccountRegisterView.vue文件實(shí)現(xiàn),實(shí)現(xiàn)方式類似于同目錄下的UserAccountLoginView.vue
可以直接把登錄頁(yè)面的樣式復(fù)制過(guò)來(lái)再做修改。
template
ContentField
div
div
form@submit.prevent="register"
div
labelfor="username"用戶名/label
inputv-model="username"type="text"id="username"placeholder="請(qǐng)輸入用戶名"
/div
div
labelfor="password"密碼/label
inputv-model="password"type="password"id="password"placeholder="請(qǐng)輸入密碼"
/div
div
labelfor="confirmedpassword"密碼/label
inputv-model="confirmedpassword"type="password"id="confirmedpassword"placeholder="請(qǐng)?jiān)俅屋斎朊艽a"
/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
實(shí)現(xiàn)效果圖:
在測(cè)試的時(shí)候可以會(huì)遇到不輸入密碼也可以注冊(cè)成功的bug,在RegisterServiceImpl.java下修改一下就可以了。
3.實(shí)現(xiàn)登錄狀態(tài)的持久化
當(dāng)我們的用戶重定向到登陸頁(yè)面的時(shí)候,我們需要把用戶的token存儲(chǔ)到瀏覽器的localstorage,這樣就可以實(shí)現(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="用戶名或密碼錯(cuò)誤";
return{
username,
password,
error_message,
login,
/script
優(yōu)化前端
在實(shí)現(xiàn)前端登錄狀態(tài)持久化之后,刷新頁(yè)面可能會(huì)存在明顯的轉(zhuǎn)換,所以下面對(duì)前端頁(yè)面進(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="請(qǐng)輸入用戶名"
/div
div
labelfor="password"密碼/label
inputv-model="password"type="password"id="password"placeholder="請(qǐng)輸入密碼"
/div
div{{error_message}}/div
buttontype="submit"提交/button
/form
/div
/div
/ContentField
/template
script
setup(){
conststore=useStore();
letusername=ref('');
letpassword=ref('');
leterror
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 大秦醫(yī)院面試題及答案
- C語(yǔ)言基礎(chǔ)選擇測(cè)試題含多知識(shí)點(diǎn)考察及答案
- 感控護(hù)士院感防控知識(shí)試題及答案
- 新疆成人考試真題及答案
- 成都三基試題題庫(kù)附答案
- 市事業(yè)單位招聘考試公共基礎(chǔ)知識(shí)試題題庫(kù)附答案詳解
- 輸血三基考試試題及答案
- 三級(jí)醫(yī)院護(hù)士招聘面試題含答案
- 嵌入式開發(fā)面試題及答案
- 河南專升本試題及答案
- 起重設(shè)備安全使用指導(dǎo)方案
- 江蘇省揚(yáng)州市區(qū)2025-2026學(xué)年五年級(jí)上學(xué)期數(shù)學(xué)期末試題一(有答案)
- 干部履歷表(中共中央組織部2015年制)
- GB/T 5657-2013離心泵技術(shù)條件(Ⅲ類)
- GB/T 3518-2008鱗片石墨
- GB/T 17622-2008帶電作業(yè)用絕緣手套
- GB/T 1041-2008塑料壓縮性能的測(cè)定
- 400份食物頻率調(diào)查問卷F表
- 滑坡地質(zhì)災(zāi)害治理施工
- 實(shí)驗(yàn)動(dòng)物從業(yè)人員上崗證考試題庫(kù)(含近年真題、典型題)
- 可口可樂-供應(yīng)鏈管理
評(píng)論
0/150
提交評(píng)論