亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue 401配合Vuex防止多次彈框的案例

 更新時間:2020年11月11日 11:39:33   作者:m0_37616866  
這篇文章主要介紹了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)
 
// 開發(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

export default {
 tokenStatus: true, // token狀態(tài)
}

5. 編輯mutations.js

export default {
 updateTokenStatus (state, bool) {
  state.tokenStatus = bool
 }
}

PS: getters用于計算屬性,actions用于異步操作(暫無使用)

6. 掛載到vue根目錄下,編輯main.js

import store from './store/store'
 
new Vue({
 store,
 router,
 render: h => h(App)
}).$mount('#app')

7. login 登錄時,改變state.tokenStatus的值

import { mapMutations } from 'vuex' 
methods: {
  // 聲明Vuex的mutations的方法
  ...mapMutations(['updateTokenStatus']),
  // 登錄方法
  login () {
    ......
    // 改變Vuex.state.tokenStatus的值
    this.updateTokenStatus(true)
  }
}

8. 配置axios的錯誤判斷

// 初始化用戶信息
  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. 攔截響應(yīng), 處理401,返回自定義錯誤

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)是問題

我就廢話不多說了,大家還是直接看代碼吧~

需要關(guān)閉嚴格模式,不然會報錯

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)閉嚴格模式
 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防止多次彈框的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • keep-alive include和exclude無效問題及解決

    keep-alive include和exclude無效問題及解決

    這篇文章主要介紹了keep-alive include和exclude無效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue之Watcher源碼解析(1)

    Vue之Watcher源碼解析(1)

    這篇文章主要為大家詳細介紹了Vue源碼之Watcher的基礎(chǔ)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • el-checkbox-group?的v-model無法綁定對象數(shù)組的問題解決

    el-checkbox-group?的v-model無法綁定對象數(shù)組的問題解決

    elementUI官方文檔中el-checkbox-group組件綁定的都為一維數(shù)組,本文主要介紹了解決el-checkbox-group?的v-model無法綁定對象數(shù)組,感興趣的可以了解一下
    2023-05-05
  • vue使用$store.commit() undefined報錯的解決

    vue使用$store.commit() undefined報錯的解決

    這篇文章主要介紹了vue使用$store.commit() undefined報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Vue3中的Token失效攔截處理方式

    Vue3中的Token失效攔截處理方式

    這篇文章主要介紹了Vue3中的Token失效攔截處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue中引入使用patch-package為依賴打補丁問題

    Vue中引入使用patch-package為依賴打補丁問題

    這篇文章主要介紹了Vue中引入使用patch-package為依賴打補丁問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 詳解vue 表單綁定與組件

    詳解vue 表單綁定與組件

    這篇文章主要介紹了vue 表單綁定與組件的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-03-03
  • 在Vue項目中取消ESLint代碼檢測的步驟講解

    在Vue項目中取消ESLint代碼檢測的步驟講解

    今天小編就為大家分享一篇關(guān)于在Vue項目中取消ESLint代碼檢測的步驟講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • mpvue寫一個CPASS小程序的示例

    mpvue寫一個CPASS小程序的示例

    這篇文章主要介紹了mpvue寫一個CPASS小程序的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue項目使用可選鏈操作符編譯報錯問題及解決

    vue項目使用可選鏈操作符編譯報錯問題及解決

    這篇文章主要介紹了vue項目使用可選鏈操作符編譯報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論