Reputation: 298
I'm trying to make a general service for API using axios
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 action
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)
}
})
error I'm getting*
_services_api__WEBPACK_IMPORTED_MODULE_0__.post is not a function
at Object.login (authenticationService.js:6)
When I expand:
login: function login(credentials) {
return _services_api__WEBPACK_IMPORTED_MODULE_0__["post"]('...', credentials);
}
Looks that it doesnt import correctly api function which creates axios ?
Upvotes: 1
Views: 3081
Reputation: 22758
You are exporting arrow function but do not call it. Try this:
import api from '@/services/api.js'
export default {
login(credentials) {
return api().post('...', credentials)
}
}
Upvotes: 3