I am trying to use Axios in my vue.js project and I want to send HTTP requests.
I read Axios socumentation in github and checked lots of samples on the web but I couldn't find my answer.
I want to define a config file and read requests path from it and call it with Axios. There are lots of APIs that I need to call and prefer to keep it in a separate file. I do not want to use axios.get or axios.post instead of that prefer to use this style:
// in my APIs file
export default {
GetAll: {
method: 'get',
url: '/Book/GetAll'
},
GetById: {
method: 'GET',
url: 'Book/GetById/{id}'
},
Add: {
method: 'POST',
url: 'Book/Add'
}
}
// Axios instantiation
import Axios from 'axios'
Vue.use({
Axios
})
const Server = Axios.create({
baseURL: myUrl
})
export default Server
// in my component
import Server from './server'
import Api from './api'
export default {
async mounted() {
var list = (await Server.request(Api.GetAll)).data
var book = (await Server.request(Api.GetById)).data
}
}
In the component I can get the list but I don't know how I can call GetById API.
'Book/GetById/{id}'? Ignoring the implementation details for a second, how would you like the call from the component to look? How would you expect other things to work, like passing parameters or body data along with the request? It feels like you've picked an architecture before considering the requirements. If you can sketch out what the calls should look like for all of these relevant considerations then perhaps someone can suggest a way of implementing it.vue resourcebefore and it was much easier to send the requests but it seemsvue resourceis retired now. I expect something to send get request with parameteridlikeBook/GetById/1or for post request, put it in request body. I hope this can help.await Server.request(Api.GetAll)orawait Server.request(Api.GetById, 2)or something like that.