欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

從eleUI的Loading知道了單例模式的用法

 更新時間:2024年01月24日 15:04:59   作者:土豆Coder  
這篇文章主要介紹了從eleUI的Loading知道了單例模式的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

從eleUI的Loading知道了單例模式

現(xiàn)象

在使用elementUI開始一個vue項目,

為了更好的用戶體驗,需要在接口請求的時候添加全局的Loading

所以在接口處理文件中,

在請求攔截的時候添加Loading,在響應(yīng)攔截的時候取消Loading

import axios from 'axios'
import { Message, Loading } from 'element-ui'

let instance = axios.create({
  baseURL: '',
  timeout: 60000
})

let loadingInstance = null

// 請求攔截
instance.interceptors.request.use((config) => {
  loadingInstance = Loading.service({
    fullscreen: true,
    text: '拼命加載中...',
    background: 'rgba(0, 0, 0, 0.8)'
  })
  return config
}, (error) => {
  loadingInstance.close()
  Message.error({message: '請求超時!'})
  return Promise.reject(error)
})

// 響應(yīng)攔截
instance.interceptors.response.use((response) => {
  loadingInstance.close()
  if (response.data && response.data.code && response.data.code === 200) {
    return response.data
  } else {
    Message({
      message: response.data.msg || '接口錯誤',
      type: 'error'
    })
  }
}, (error) => {
  loadingInstance.close()
  return Promise.reject(error)
})

export default instance

優(yōu)化

邏輯似乎很合理,但是實際項目中發(fā)現(xiàn),如果某個頁面請求多個接口,且每個接口都返回很慢的話,實際看到的效果是雖然Loading會出現(xiàn),但是當?shù)谝粋€接口返回值以后后面的Loading都不會出現(xiàn)了,就會出現(xiàn)頁面數(shù)據(jù)從無到有的過濾,用戶體驗較差。

原來,是因為elementUI的全屏Loading是單例的:如果前一個Loading關(guān)閉之前再次調(diào)用了下一個Loading并不會創(chuàng)建一個新的實例,返回的仍然是當前這個Loading實例;同理,當調(diào)用任意一個close()方法都會關(guān)閉這個Loading實例。

因為這幾個接口都是同一時間請求的,也就是說當前頁面幾個Loading實例其實都是同一個,所以關(guān)閉后也就都關(guān)閉了。

改進后:

import axios from 'axios'
import { Message, Loading } from 'element-ui'

let instance = axios.create({
  baseURL: '',
  timeout: 60000
})

/* 當頁面有兩個接口時,第一個接口loading的close事件會直接將第二個接口的loading實例也close */
let loadingInstance = null

function startLoading () {
  loadingInstance = Loading.service({
    fullscreen: true,
    text: '拼命加載中...',
    background: 'rgba(0, 0, 0, 0.8)'
  })
}

function endLoading () {
  loadingInstance.close()
}

let needLoadingRequestCount = 0

function showFullScreenLoading () {
  if (needLoadingRequestCount === 0) {
    startLoading()
  }
  needLoadingRequestCount++
}

function tryHideFullScreenLoading () {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    endLoading()
  }
}

// 請求攔截
instance.interceptors.request.use((config) => {
  showFullScreenLoading()
  return config
}, (error) => {
  tryHideFullScreenLoading()
  Message.error({message: '請求超時!'})
  return Promise.reject(error)
})

// 響應(yīng)攔截
instance.interceptors.response.use((response) => {
  tryHideFullScreenLoading()
  if (response.data && response.data.code && response.data.code === 200) {
    return response.data
  } else {
    Message({
      message: response.data.msg || '接口錯誤',
      type: 'error'
    })
  }
}, (error) => {
  tryHideFullScreenLoading()
  return Promise.reject(error)
})

export default instance

每次創(chuàng)建Loading實例的時候判斷當前是否存在,如果當前還沒有Loading實例就創(chuàng)建一個,如果有就不會再創(chuàng)建而是計數(shù);

每次關(guān)閉的時候判斷當前的計數(shù),如果是0了就關(guān)閉,否則也計數(shù)減一,直到為0的時候表示當前所有頁面所有接口都返回結(jié)束了,此時執(zhí)行關(guān)閉Loading.close()操作關(guān)閉菊花。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論