1.安裝Vuex
npm install vuex --save
2. 新建store目錄結構
3. 編輯store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import Vuex from 'vuex' import Vue from 'vue' import defaultState from './state/state' import mutations from './mutations/mutations' import getters from './getters/getters' import actions from './actions/actions' Vue.use(Vuex) // 開發(fā)環(huán)境 const isDev = process.env.NODE_ENV === 'development' export default new Vuex.Store({ strict: isDev, // 開發(fā)環(huán)境中使用嚴格模式,防止給Vuex的狀態(tài)對象直接賦值 state: defaultState, mutations, getters, actions }) |
4. 編輯state.js
1
2
3
|
export default { tokenStatus: true , // token狀態(tài) } |
5. 編輯mutations.js
1
2
3
4
5
|
export default { updateTokenStatus (state, bool) { state.tokenStatus = bool } } |
PS: getters用于計算屬性,actions用于異步操作(暫無使用)
6. 掛載到vue根目錄下,編輯main.js
1
2
3
4
5
6
7
|
import store from './store/store' new Vue({ store, router, render: h => h(App) }).$mount( '#app' ) |
7. login 登錄時,改變state.tokenStatus的值
1
2
3
4
5
6
7
8
9
10
11
|
import { mapMutations } from 'vuex' methods: { // 聲明Vuex的mutations的方法 ...mapMutations([ 'updateTokenStatus' ]), // 登錄方法 login () { ...... // 改變Vuex.state.tokenStatus的值 this .updateTokenStatus( true ) } } |
8. 配置axios的錯誤判斷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 初始化用戶信息 initUserInfo () { const p1 = this .$api.user.getUserInfo() p1.then(result => { this .data = result this .isEdit = false this .firstLoading = false }). catch (reason => { this .firstLoading = false this .isEdit = false // 目前后端是通過code為-1,返回錯誤信息 if (parseInt(reason.code) === -1) { this .$alert(reason.message, '提示' , { type: 'error' }) } }) }, |
9. 攔截響應, 處理401,返回自定義錯誤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import router from '../../router' import axios from 'axios' import localStorage from 'localStorage' import { MessageBox } from 'element-ui' import store from '../../store/store' // http response 攔截器 axios.interceptors.response.use( response => { return response }, error => { if (error.response) { if (error.response.status === 401) { switch (error.response.status) { case 401: const route = localStorage.getItem( 'vip_entrance' ) router.replace({ path: route, query: { redirect: router.currentRoute.fullPath } }) if (store.state.tokenStatus) { // 餓了么框架彈框 MessageBox.alert( '登錄超時!' , '提示' , { type: 'error' }) // 修改tokenStatus狀態(tài),防止多次點擊 store.commit( 'updateTokenStatus' , false ) } const data = { code: 1 } return Promise.reject(data) } } } return Promise.reject(error.response.data) } ) |
補充知識:vue 配置vuex在嚴格模式下出現(xiàn)是問題
我就廢話不多說了,大家還是直接看代碼吧~
需要關閉嚴格模式,不然會報錯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import Vue from "vue" ; import Vuex from "vuex" ; import createPersistedState from "vuex-persistedstate" ; import * as Cookies from "js-cookie" ; import user from "./modules/user" ; import myCen from "./modules/myCen" ; import registered from "./modules/registered" ; Vue.use(Vuex); export default new Vuex.Store({ strict: false , //關閉嚴格模式 modules: { user, myCen, registered }, // 持久化儲存 plugins: [ createPersistedState({ storage: { getItem: key => Cookies.get(key), setItem: (key, value) => Cookies.set(key, value, { expires: 7 }), removeItem: key => Cookies.remove(key) } }) ] }); |
以上這篇Vue 401配合Vuex防止多次彈框的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/m0_37616866/article/details/89086045