vuex + axios 做登錄驗(yàn)證 并且保存登錄狀態(tài)的實(shí)例
還是那句話,網(wǎng)上找個(gè)完整的博客真的難,實(shí)現(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ù)進(jìn)行的處理
//組件中采用this.$store.commit('方法名') 的方式調(diào)用,實(shí)現(xiàn)充分解耦
//內(nèi)部操作必須在此刻完成(同步)
[ADD_COUNT] (state, token) { // 第一個(gè)參數(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/>'
}) ;
第三步:進(jìn)行登錄 操作,調(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('登錄失敗,錯(cuò)誤:'+ err);
this.$Loading.error();
}.bind(this))
差點(diǎn)忘記了一點(diǎn),在router中要配置需要驗(yàn)證是否登錄的請求

附上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, // 添加該字段,表示進(jìn)入這個(gè)路由是需要登錄的
},
},
{ path: '*', component: P404 } //這里是保證錯(cuò)誤地址會跳轉(zhuǎn)到404界面進(jìn)行提示
]
})
這些方法的編寫順序可能沒有體現(xiàn)出來,不過最終效果就是這個(gè)了,如果有疑問歡迎留言。
以上這篇vuex + axios 做登錄驗(yàn)證 并且保存登錄狀態(tài)的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue 實(shí)現(xiàn)登錄界面驗(yàn)證碼功能
- vue實(shí)現(xiàn)登錄頁面的驗(yàn)證碼以及驗(yàn)證過程解析(面向新手)
- vue 登錄滑動驗(yàn)證實(shí)現(xiàn)代碼
- 使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能
- Vue實(shí)戰(zhàn)之vue登錄驗(yàn)證的實(shí)現(xiàn)代碼
- Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法
- vue登錄注冊及token驗(yàn)證實(shí)現(xiàn)代碼
- vue router+vuex實(shí)現(xiàn)首頁登錄驗(yàn)證判斷邏輯
- Vue中登錄驗(yàn)證成功后保存token,并每次請求攜帶并驗(yàn)證token操作
- vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證
相關(guān)文章
Vue中Axios配置不同的baseURL,請求不同的域名接口方式
這篇文章主要介紹了Vue中Axios配置不同的baseURL,請求不同的域名接口方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue.js打包項(xiàng)目后頁面出現(xiàn)空白的解決辦法
這篇文章主要介紹了vue.js打包項(xiàng)目后頁面出現(xiàn)空白的解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-11-11
Vue實(shí)現(xiàn)商品分類菜單數(shù)量提示功能
這篇文章主要介紹了Vue實(shí)戰(zhàn)—商品分類菜單數(shù)量提示功能,本文通過項(xiàng)目實(shí)戰(zhàn)給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法
今天小編就為大家分享一篇在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

