vuex + axios 做登錄驗證 并且保存登錄狀態(tài)的實例
還是那句話,網(wǎng)上找個完整的博客真的難,實現(xiàn)效果全靠摸索啊
第一步:安裝axios 、vuex npm i -s axios npm i -s vuex 執(zhí)行這兩句 ,vue等環(huán)境搭建就不廢話了
第二步:配置main.js文件

上圖不上碼,菊花萬人捅,附上代碼
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import locale from 'iview/dist/locale/en-US';
import VueParticles from 'vue-particles';
import axios from 'axios' ;
import Vuex from 'vuex' //引入狀態(tài)管理
Vue.use(VueParticles) ;
Vue.use(iView, { locale });
Vue.config.productionTip = false ;
Vue.prototype.$http = axios ;
Vue.use(Vuex) ;
const ADD_COUNT = 'ADD_COUNT'; // 用常量代替事件類型,使得代碼更清晰
const REMOVE_COUNT = 'REMOVE_COUNT';
//注冊狀態(tài)管理全局參數(shù)
var store = new Vuex.Store({
state:{
token:'',
userID:'',
},
mutations: {
//寫法與getters相類似
//組件想要對于vuex 中的數(shù)據(jù)進行的處理
//組件中采用this.$store.commit('方法名') 的方式調(diào)用,實現(xiàn)充分解耦
//內(nèi)部操作必須在此刻完成(同步)
[ADD_COUNT] (state, token) { // 第一個參數(shù)為 state 用于變更狀態(tài) 登錄
sessionStorage.setItem("token", token);
state.token = token;
},
[REMOVE_COUNT] (state, token) { // 退出登錄
sessionStorage.removeItem("token", token);
state.token = token;
},
}
});
router.beforeEach((to, from, next) => {
iView.LoadingBar.start();//loadong 效果
store.state.token = sessionStorage.getItem('token');//獲取本地存儲的token
if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權(quán)限
if (store.state.token !== "") { // 通過vuex state獲取當(dāng)前的token是否存
next();
}
else {
next({
path: '/login',
query: {redirect: to.fullPath} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由
})
}
}
else {
next();
}
})
router.afterEach(route => {
iView.LoadingBar.finish();
});
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, //注冊組件
components: { App },
template: '<App/>'
}) ;
第三步:進行登錄 操作,調(diào)用main.js 中定義好的修改token的方法[ADD_COUNT]

附上請求部分代碼
this.$http({
method: 'get',
url: '/api/login',
}).then(function(res){
var json = res.data
console.log(json.data);
this.$Message.success('Success!');
this.$store.commit('ADD_COUNT', json.data.token);
let clock = window.setInterval(() => {
this.totalTime-- ;
if (this.totalTime < 0) {
window.clearInterval(clock) ;
this.$Loading.finish();
this.$router.push('/') ;
}
},1000)
}.bind(this)).catch(function(err){
this.$Message.error('登錄失敗,錯誤:'+ err);
this.$Loading.error();
}.bind(this))
差點忘記了一點,在router中要配置需要驗證是否登錄的請求

附上router/index.js 代碼
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login/Login'
import P404 from '@/components/404/404'
import Main from '@/components/Main'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/login',//登錄頁
name: 'Login',
component: Login
},
{
path: '/',//首頁
name: 'Main',
component: Main,
meta: {
requireAuth: true, // 添加該字段,表示進入這個路由是需要登錄的
},
},
{ path: '*', component: P404 } //這里是保證錯誤地址會跳轉(zhuǎn)到404界面進行提示
]
})
這些方法的編寫順序可能沒有體現(xiàn)出來,不過最終效果就是這個了,如果有疑問歡迎留言。
以上這篇vuex + axios 做登錄驗證 并且保存登錄狀態(tài)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Vue 實現(xiàn)登錄界面驗證碼功能
- vue實現(xiàn)登錄頁面的驗證碼以及驗證過程解析(面向新手)
- vue 登錄滑動驗證實現(xiàn)代碼
- 使用vue-route 的 beforeEach 實現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗證登錄)功能
- Vue實戰(zhàn)之vue登錄驗證的實現(xiàn)代碼
- Vue中的驗證登錄狀態(tài)的實現(xiàn)方法
- vue登錄注冊及token驗證實現(xiàn)代碼
- vue router+vuex實現(xiàn)首頁登錄驗證判斷邏輯
- Vue中登錄驗證成功后保存token,并每次請求攜帶并驗證token操作
- vue實現(xiàn)登錄時滑塊驗證
相關(guān)文章
Vue中Axios配置不同的baseURL,請求不同的域名接口方式
這篇文章主要介紹了Vue中Axios配置不同的baseURL,請求不同的域名接口方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

