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

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

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

從eleUI的Loading知道了單例模式

現(xiàn)象

在使用elementUI開(kāi)始一個(gè)vue項(xiàng)目,

為了更好的用戶體驗(yàn),需要在接口請(qǐng)求的時(shí)候添加全局的Loading

所以在接口處理文件中,

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

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

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

let loadingInstance = null

// 請(qǐng)求攔截
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: '請(qǐng)求超時(shí)!'})
  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 || '接口錯(cuò)誤',
      type: 'error'
    })
  }
}, (error) => {
  loadingInstance.close()
  return Promise.reject(error)
})

export default instance

優(yōu)化

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

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

因?yàn)檫@幾個(gè)接口都是同一時(shí)間請(qǐng)求的,也就是說(shuō)當(dāng)前頁(yè)面幾個(gè)Loading實(shí)例其實(shí)都是同一個(gè),所以關(guān)閉后也就都關(guān)閉了。

改進(jìn)后:

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

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

/* 當(dāng)頁(yè)面有兩個(gè)接口時(shí),第一個(gè)接口loading的close事件會(huì)直接將第二個(gè)接口的loading實(shí)例也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()
  }
}

// 請(qǐng)求攔截
instance.interceptors.request.use((config) => {
  showFullScreenLoading()
  return config
}, (error) => {
  tryHideFullScreenLoading()
  Message.error({message: '請(qǐng)求超時(shí)!'})
  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 || '接口錯(cuò)誤',
      type: 'error'
    })
  }
}, (error) => {
  tryHideFullScreenLoading()
  return Promise.reject(error)
})

export default instance

每次創(chuàng)建Loading實(shí)例的時(shí)候判斷當(dāng)前是否存在,如果當(dāng)前還沒(méi)有Loading實(shí)例就創(chuàng)建一個(gè),如果有就不會(huì)再創(chuàng)建而是計(jì)數(shù);

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

總結(jié)

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

相關(guān)文章

最新評(píng)論