欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue注冊模塊與登錄狀態(tài)的持久化實現(xiàn)方法詳解

 更新時間:2022年08月30日 08:33:47   作者:蕪湖男童  
這篇文章主要介紹了Vue注冊模塊與登錄狀態(tài)的持久化實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

整體框架

1. 前端頁面授權(quán)

當我們登錄網(wǎng)站的時候,如果沒有登錄,強制讓用戶重定向到 登錄界面

router 目錄下的 index.js 文件下實現(xiàn)。 router -> index.js

import store from '../store/index'
// 把一些額外信息放到一個額外的域里面,meta信息里面存一下是否要授權(quán),如果需要授權(quán)而且沒有登錄,重定向到登錄頁面,重定向到登錄界面。
const routes = [
  {
    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),點擊 除注冊之外的按鈕 頁面會跳轉(zhuǎn)到 登錄界面。

2. 實現(xiàn)注冊頁面

view -> user -> account 下的 UserAccountRegisterView.vue 文件實現(xiàn),實現(xiàn)方式類似于同目錄下的 UserAccountLoginView.vue

可以直接把登錄頁面的樣式復制過來再做修改。

<template>
    <ContentField>
        <div class="row justify-content-md-center">
            <div class="col-3">
                <form @submit.prevent="register">
                    <div class="mb-3">
                        <label for="username" class="form-label">用戶名</label>
                        <input v-model="username" type="text" class="form-control" id="username" placeholder="請輸入用戶名">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密碼</label>
                        <input v-model="password" type="password" class="form-control" id="password" placeholder="請輸入密碼">
                    </div>
                    <div class="mb-3">
                        <label for="confirmedpassword" class="form-label">密碼</label>
                        <input v-model="confirmedpassword" type="password" class="form-control" id="confirmedpassword" placeholder="請再次輸入密碼">
                    </div>
                    <div class="error-message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
            </div>
        </div>
    </ContentField>
</template>
<script>
import ContentField from '../../../components/ContentField.vue'
import { ref } from 'vue'
import router from '../../../router/index'
import $ from 'jquery'
export default {
    components: {
        ContentField
    },
    setup() {
        let username = ref('');
        let password = ref('');
        let confirmedPassword = ref('');
        let error_message = ref('');
        const register = () => {
            $.ajax({
                url: "http://127.0.0.1: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>
<style scoped>
button {
    width: 100%;
}
div.error-message {
    color: red;
    justify-content: center;
}
</style>

實現(xiàn)效果圖:

在測試的時候可以會遇到不輸入密碼也可以注冊成功的 bug, 在 RegisterServiceImpl.java 下 修改一下就可以了。

3. 實現(xiàn)登錄狀態(tài)的持久化

當我們的用戶重定向到登陸頁面的時候,我們需要把用戶的 token 存儲到瀏覽器的 local storage,這樣就可以實現(xiàn)登錄狀態(tài)持久化。

首先 修改 store 目錄下的 -> user.js 文件,在合適的位置添加下列兩行。

localStorage.setItem("jwt_token", resp.token);
localStorage.removeItem("jwt_token");

其次 修改 view -> user -> account 下的 UserAccountLoginView.vue 文件

<script>
import ContentField from '../../../components/ContentField.vue'
import { useStore } from 'vuex'
import { ref } from 'vue'
import router from '../../../router/index'
export default {
    components: {
        ContentField
    },
    setup() {
        const store = useStore();
        let username = ref('');
        let password = ref('');
        let error_message = ref('');
        const jwt_token = localStorage.getItem("jwt_token");
        if (jwt_token) {
            store.commit("updateToken", jwt_token);
            store.dispatch("getinfo", {
                success() {
                    router.push({ name: "home" });
                },
                error() {
                }
            })
        }else {
        }
        const login = () => {
            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)換,所以下面對前端頁面進行優(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>
    <ContentField v-if="!$store.state.user.pulling_info">
        <div class="row justify-content-md-center">
            <div class="col-3">
                <form @submit.prevent="login">
                    <div class="mb-3">
                        <label for="username" class="form-label">用戶名</label>
                        <input v-model="username" type="text" class="form-control" id="username" placeholder="請輸入用戶名">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密碼</label>
                        <input v-model="password" type="password" class="form-control" id="password" placeholder="請輸入密碼">
                    </div>
                    <div class="error-message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
            </div>
        </div>
    </ContentField>
</template>
<script>
setup() {
        const store = useStore();
        let username = ref('');
        let password = ref('');
        let error_message = ref('');
        const jwt_token = localStorage.getItem("jwt_token");
        if (jwt_token) {
            store.commit("updateToken", jwt_token);
            store.dispatch("getinfo", {
                success() {
                    router.push({ name: "home" });
                    store.commit("updatePullingInfo", false);
                },
                error() {
                    store.commit("updatePullingInfo", false);
                }
            })
        }else {
            store.commit("updatePullingInfo", false);
        }
}
</script>

最后還需要修改 NavBar.vue。

<ul class="navbar-nav" v-else-if="!$store.state.user.pulling_info">
  <li class="nav-item">
    <router-link class="nav-link" :to="{name: 'user_account_login' }" role="button">
      登錄
    </router-link>
  </li>
  <li class="nav-item">
    <router-link class="nav-link" :to="{name: 'user_account_register'}" role="button">
      注冊
    </router-link>
  </li>
</ul>

代碼地址

king of bots

到此這篇關(guān)于Vue注冊模塊與登錄狀態(tài)的持久化實現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Vue注冊模塊與登錄狀態(tài)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • elementUI樣式修改未生效問題詳解(掛載到了body標簽上)

    elementUI樣式修改未生效問題詳解(掛載到了body標簽上)

    vue+elementUI項目開發(fā)中,經(jīng)常遇到修改elementUI組件樣式無效的問題,這篇文章主要給大家介紹了關(guān)于elementUI樣式修改未生效問題的相關(guān)資料,掛載到了body標簽上,需要的朋友可以參考下
    2023-04-04
  • 記一次用vue做的活動頁的方法步驟

    記一次用vue做的活動頁的方法步驟

    這篇文章主要介紹了記一次用vue做的活動頁的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue使用SVG實現(xiàn)圓形進度條音樂播放

    vue使用SVG實現(xiàn)圓形進度條音樂播放

    這篇文章主要為大家詳細介紹了vue使用SVG實現(xiàn)圓形進度條音樂播放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vuex中的state、getters、mutations、actions之間的關(guān)系解讀

    vuex中的state、getters、mutations、actions之間的關(guān)系解讀

    這篇文章主要介紹了vuex中的state、getters、mutations、actions之間的關(guān)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 在vue項目實現(xiàn)一個ctrl+f的搜索功能

    在vue項目實現(xiàn)一個ctrl+f的搜索功能

    剛剛接到領(lǐng)導通知,需要實現(xiàn)搜索功能,因為項目是vue的而且是手機端,對我來說有點小難度。經(jīng)過小編的一番思索最終還是解決了,今天小編把實現(xiàn)過程分享到腳本之家平臺,需要的朋友參考下
    2020-02-02
  • vue中傳參params和data的區(qū)別

    vue中傳參params和data的區(qū)別

    本文主要介紹了vue中傳參params和data的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Nuxt.js Vue服務端渲染摸索

    詳解Nuxt.js Vue服務端渲染摸索

    本篇文章主要介紹了詳解Nuxt.js Vue服務端渲染摸索,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • vue項目中使用Hbuilder打包app 設置沉浸式狀態(tài)欄的方法

    vue項目中使用Hbuilder打包app 設置沉浸式狀態(tài)欄的方法

    這篇文章主要介紹了vue項目 使用Hbuilder打包app 設置沉浸式狀態(tài)欄的方法,本文通過實例代碼效果圖展示給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-10-10
  • 詳解Vue3中Watch監(jiān)聽事件的使用

    詳解Vue3中Watch監(jiān)聽事件的使用

    這篇文章主要為大家詳細介紹了Vue3中Watch監(jiān)聽事件的使用的相關(guān)資料,文中的示例代碼講解詳細,對我們學習Vue3有一定的幫助,需要的可以參考一下
    2023-02-02
  • 搭建vue3項目以及按需引入element-ui框架組件全過程

    搭建vue3項目以及按需引入element-ui框架組件全過程

    element是基于vue.js框架開發(fā)的快速搭建前端的UI框架,下面這篇文章主要給大家介紹了關(guān)于搭建vue3項目以及按需引入element-ui框架組件的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下
    2024-02-02

最新評論