Reputation: 288
I am new to vuejs, i am using different components, and in every components i have made a API call, the API used is similar in every components, but only change in id i.e. if API is /contents/info/[id], I have used the respective id for the respective component.
But in order to make it effective, i have few questions regarding that:
1.How can i put the /contents/info/[id] API in a JS file, where [id] should be the argument. If i want to make an API call in any component i just want to use the id.
2.After making a API call in a JS file, i want to know how it can be used in any component by their respective id ?
Please do help me, because i have many APIs which only differs by id. And i want to make it effective by just creating a single JS file and dumping all the APIs with arguments and then importing that to a respective component and use it by their respective id.
Upvotes: 0
Views: 628
Reputation: 76
you can create an api_service and put in plugins(or another folder like utils or services if you want) folder and inject that to all of you and config that in your nuxt.config.js. like below:
index.js in plugins folder:
var _this
export default function ({ app }, inject) {
_this = app
inject('ApiService', ApiService)
}
const ApiService = {
post (resource, params) {
return _this.$axios.$post(`${resource}`, params)
}
}
and you can access it in all of your project.
Upvotes: 2