javascript - 多個(gè)axios同時(shí)請(qǐng)求,數(shù)據(jù)被前面的請(qǐng)求覆蓋,如何解決?
問(wèn)題描述
多個(gè)axios同時(shí)請(qǐng)求,數(shù)據(jù)被前面的請(qǐng)求覆蓋,如何解決?用axios.all能解決,但總不能每次都加個(gè)all方法,不方便axios配置
import axios from ’axios’import store from ’../vuex/’import { Notification } from ’element-ui’// import router from ’../routers’// axios 配置axios.defaults.timeout = 10000axios.defaults.baseURL = ’/api/’// http request 攔截器axios.interceptors.request.use(config => { if (store.state.token) {} return config}, err => { return Promise.reject(err)})// 后臺(tái)無(wú)返回success因此修改之前的攔截器規(guī)則,直接返回?cái)?shù)據(jù)// http response 攔截器axios.interceptors.response.use(response => { console.log(response) if (response.data.status === 200 || response.data.code === 200) { return response.data.data } else { Notification.error(response.statusText) return response.data // return Promise.reject(response.statusText) }})export default axios
api配置(有幾個(gè)如下的API,不全部貼了)
// 1. 產(chǎn)品系列 :: 列表export const getProductType = params => { return axios.get(`/product/type/list`, params)}
頁(yè)面中調(diào)用vuex
store.dispatch(’getProductStatus’) store.dispatch(’getProductStyle’) store.dispatch(’getProductType’)
vuex配置
import {getProductStyle, getProductStatus, getProductType} from ’@/http/api’const state = { panelIsShow: false, dict: { statusDict: [], styleDict: [], typeDic: [] }}const mutations = { SET_PANEL_SHOW (state, data) { state.panelIsShow = data }, // 獲取產(chǎn)品募集狀態(tài)字典 GET_DICT_STATUS (state, dict) { state.dict.statusDict = dict }, // 獲取產(chǎn)品風(fēng)格字典 GET_DICT_STYLE (state, dict) { state.dict.styleDict = dict }, // 獲取產(chǎn)品系列字典 GET_DICT_TYPE (state, dict) { state.dict.typeDict = dict }}const actions = { // 產(chǎn)品風(fēng)格 async getProductStyle ({commit}) { let data = await getProductStyle() commit(’GET_DICT_STYLE’, data) }, // 產(chǎn)品募集狀態(tài) async getProductStatus ({commit}) { let data = await getProductStatus() commit(’GET_DICT_STATUS’, data) }, // 產(chǎn)品系列 async getProductType ({commit}) { let data = await getProductType() commit(’GET_DICT_TYPE’, data) }}const getters = { panelIsShow: state => state.panelIsShow, dict: (state) => state.dict}export default { state, getters, actions, mutations}
問(wèn)題解答
回答1:不會(huì)啊。
貼代碼吧。
這種寫(xiě)法,肯定是要被覆蓋了
回答2:你要是不用all方法當(dāng)然不能保證順序,數(shù)據(jù)當(dāng)然會(huì)被覆蓋。
