在vuex存储操作中更改默认axios基本url失败

时间:2019-02-07 16:43:07

标签: javascript vuejs2 vuex quasar-framework

我想在vuex操作中更改默认的baseUrl。

在我的配置中,我有

const axiosInstance = axios.create({
   baseURL: appdetails.apiurl
})

axiosInstance.interceptors .....

export {axiosInstance}

所以在我的类星体商店中,我只是打电话给

import { axiosInstance } from '../../../plugins/axios'

function getAverages (context) {

  return new Promise((resolve, reject) => {
    return axiosInstance.get("average-items")
        .then((res) => {
            ......//other stuff
        })
   })
}

在商店中,上述作品

但是现在我想从导入的axiosInstance更改baseUrl。如何进行。

所以我尝试了。我在vuex动作中

function getAppValues (context, { baseUrl, apiurl}) {
  let fetchreq = axiosInstance.get("test");
  if (baseUrl){
    fetchreq = axiosInstance.get({ url: apiurl, baseURL: baseUrl });
   }
  return new Promise((resolve, reject) => {
    return fetchreq
        .then((res) => {
            console.log("res is", res);
            resolve(true)
        },(err)=>{
            console.log("err is e", err);
        })
  })

}

所以在我的组件中打电话

 this.$store.dispatch("app/getAppValues",{baseUrl:'https://test.com',apiurl:'test'});

但是现在出现错误

res is TypeError: request.url.indexOf is not a function
at eval 

另外,当我没有像this.$store.dispatch("app/getAppValues")这样传递baseUrl时却遇到错误

caught (in promise) TypeError: Cannot read property 'baseUrl' of undefined. Even when the request is an error it still resolves as a response

如何解决此问题,同时检查是否在我的商店操作请求中传递了baseUrl

0 个答案:

没有答案
相关问题