javascript - Bei mehreren Axios-Anfragen gleichzeitig werden die Daten durch die vorherige Anfrage überschrieben. Wie lässt sich das Problem lösen?
大家讲道理
大家讲道理 2017-07-05 11:05:31
0
2
1313

Bei mehreren Axios-Anfragen gleichzeitig werden die Daten durch die vorherige Anfrage überschrieben. Wie lässt sich das Problem lösen?
Die Verwendung von axios.all kann das Problem lösen, aber Sie können nicht immer eine All-Methode hinzufügen, was unpraktisch ist
Axios-Konfiguration

import axios from 'axios'
import store from '../vuex/'
import { Notification } from 'element-ui'
// import router from '../routers'
// axios 配置
axios.defaults.timeout = 10000
axios.defaults.baseURL = '/api/'
// http request 拦截器
axios.interceptors.request.use(config => {
  if (store.state.token) {}
  return config
}, err => {
  return Promise.reject(err)
})

// 后台无返回success因此修改之前的拦截器规则,直接返回数据
// 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-Konfiguration (es gibt mehrere APIs wie folgt, nicht alle veröffentlicht)

// 1. 产品系列 :: 列表
export const getProductType = params => {
  return axios.get(`/product/type/list`, params)
}

Vuex auf der Seite aufrufen

  store.dispatch('getProductStatus')
  store.dispatch('getProductStyle')
  store.dispatch('getProductType')

vuex-Konfiguration

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
  },
    // 获取产品募集状态字典
  GET_DICT_STATUS (state, dict) {
    state.dict.statusDict = dict
  },
  // 获取产品风格字典
  GET_DICT_STYLE (state, dict) {
    state.dict.styleDict = dict
  },
  // 获取产品系列字典
  GET_DICT_TYPE (state, dict) {
    state.dict.typeDict = dict
  }
}
const actions = {
  // 产品风格
  async getProductStyle ({commit}) {
    let data = await getProductStyle()
    commit('GET_DICT_STYLE', data)
  },
  // 产品募集状态
  async getProductStatus ({commit}) {
    let data = await getProductStatus()
    commit('GET_DICT_STATUS', data)
  },
  // 产品系列
  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
}
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

Antworte allen(2)
滿天的星座

不会啊。

贴代码吧。

这种写法,肯定是要被覆盖了

刘奇

你要是不用all方法当然不能保证顺序,数据当然会被覆盖。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!