【问题标题】:WEBPACK_IMPORTED_MODULE_0__.post is not a function -> Axios create then postWEBPACK_IMPORTED_MODULE_0__.post 不是函数 -> Axios 创建然后发布
【发布时间】:2020-06-24 19:36:29
【问题描述】:

我正在尝试使用 axios 为 API 提供通用服务

api.js

import axios from 'axios'

export default () => {
    return axios.create({
        baseURL: `${window.location.origin}/`
    })  
} 

authenticationService.js

import api from '@/services/api.js'

export default {
    login(credentials) {
        return api.post('...', credentials)
    }
  }   

vuex 动作

import authenticationService from '@/services/authentication/authenticationService'

async login({commit}, credentials) {
    try {
        let response = await authenticationService.login(credentials)
        console.log(response)
    } catch(er) {
        console.log(er)
    }
})

我遇到了错误*

_services_api__WEBPACK_IMPORTED_MODULE_0__.post is not a function
    at Object.login (authenticationService.js:6)

当我展开时:

 login: function login(credentials) {
    return _services_api__WEBPACK_IMPORTED_MODULE_0__["post"]('...', credentials);
  }

看起来它没有正确导入创建 axios 的 api 函数?

【问题讨论】:

    标签: javascript api vue.js request axios


    【解决方案1】:

    您正在导出箭头函数,但不要调用它。 试试这个:

    import api from '@/services/api.js'
    
    export default {
        login(credentials) {
            return api().post('...', credentials)
        }
      }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 2018-07-18
      • 2021-12-14
      • 2018-12-23
      • 1970-01-01
      • 2022-01-21
      • 2013-07-04
      相关资源
      最近更新 更多