Vue 401配合Vuex防止多次彈框的案例
1.安裝Vuex
npm install vuex --save
2. 新建store目錄結(jié)構(gòu)
3. 編輯store.js
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) // 開(kāi)發(fā)環(huán)境 const isDev = process.env.NODE_ENV === 'development' export default new Vuex.Store({ strict: isDev, // 開(kāi)發(fā)環(huán)境中使用嚴(yán)格模式,防止給Vuex的狀態(tài)對(duì)象直接賦值 state: defaultState, mutations, getters, actions })
4. 編輯state.js
export default { tokenStatus: true, // token狀態(tài) }
5. 編輯mutations.js
export default { updateTokenStatus (state, bool) { state.tokenStatus = bool } }
PS: getters用于計(jì)算屬性,actions用于異步操作(暫無(wú)使用)
6. 掛載到vue根目錄下,編輯main.js
import store from './store/store' new Vue({ store, router, render: h => h(App) }).$mount('#app')
7. login 登錄時(shí),改變state.tokenStatus的值
import { mapMutations } from 'vuex' methods: { // 聲明Vuex的mutations的方法 ...mapMutations(['updateTokenStatus']), // 登錄方法 login () { ...... // 改變Vuex.state.tokenStatus的值 this.updateTokenStatus(true) } }
8. 配置axios的錯(cuò)誤判斷
// 初始化用戶信息 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 // 目前后端是通過(guò)code為-1,返回錯(cuò)誤信息 if (parseInt(reason.code) === -1) { this.$alert(reason.message, '提示', { type: 'error' }) } }) },
9. 攔截響應(yīng), 處理401,返回自定義錯(cuò)誤
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('登錄超時(shí)!', '提示', { type: 'error' }) // 修改tokenStatus狀態(tài),防止多次點(diǎn)擊 store.commit('updateTokenStatus', false) } const data = { code: 1 } return Promise.reject(data) } } } return Promise.reject(error.response.data) } )
補(bǔ)充知識(shí):vue 配置vuex在嚴(yán)格模式下出現(xiàn)是問(wèn)題
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
需要關(guān)閉嚴(yán)格模式,不然會(huì)報(bào)錯(cuò)
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, //關(guān)閉嚴(yán)格模式 modules: { user, myCen, registered }, // 持久化儲(chǔ)存 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防止多次彈框的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問(wèn)題解析
- Vuex實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車
- 基于vuex實(shí)現(xiàn)購(gòu)物車功能
- vuex的使用和簡(jiǎn)易實(shí)現(xiàn)
- vuex的使用步驟
- vuex頁(yè)面刷新導(dǎo)致數(shù)據(jù)丟失的解決方案
- vuex Module將 store 分割成模塊的操作
- vuex的數(shù)據(jù)渲染與修改淺析
- vuex中遇到的坑,vuex數(shù)據(jù)改變,組件中頁(yè)面不渲染操作
- 解決vuex刷新數(shù)據(jù)消失問(wèn)題
- 解決vuex改變了state的值,但是頁(yè)面沒(méi)有更新的問(wèn)題
- 實(shí)現(xiàn)vuex原理的示例
- vuex刷新后數(shù)據(jù)丟失的解決方法
- 如何手寫(xiě)一個(gè)簡(jiǎn)易的 Vuex
- Vuex實(shí)現(xiàn)購(gòu)物車小功能
- vue+vuex+axios從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作
- Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例
- 一文輕松理解Vuex
相關(guān)文章
keep-alive include和exclude無(wú)效問(wèn)題及解決
這篇文章主要介紹了keep-alive include和exclude無(wú)效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11el-checkbox-group?的v-model無(wú)法綁定對(duì)象數(shù)組的問(wèn)題解決
elementUI官方文檔中el-checkbox-group組件綁定的都為一維數(shù)組,本文主要介紹了解決el-checkbox-group?的v-model無(wú)法綁定對(duì)象數(shù)組,感興趣的可以了解一下2023-05-05vue使用$store.commit() undefined報(bào)錯(cuò)的解決
這篇文章主要介紹了vue使用$store.commit() undefined報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Vue中引入使用patch-package為依賴打補(bǔ)丁問(wèn)題
這篇文章主要介紹了Vue中引入使用patch-package為依賴打補(bǔ)丁問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解
今天小編就為大家分享一篇關(guān)于在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01vue項(xiàng)目使用可選鏈操作符編譯報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目使用可選鏈操作符編譯報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03