Almost all of my components require some kind of interaction with my apiservice.js class, which imports Axios and makes appropriate http requests depending on the method called. I know this isn't normally recommended, but, in every one of my components, I have the following:
import {APIService} from '../store/APIService.js';
const apiService = new APIService();
My APIService.js looks roughly like this (trimmed down for simplicity):
import axios from 'axios';
const API_URL = 'http://myserver:82/services/locationmanagement';
export class APIService{
constructor(){
}
getLocations() {
const url = `${API_URL}/api/locations/`;
return axios.get(url).then(response => response.data);
}
saveLocation(location) {
let url = `${API_URL}/api/locations/`;
let id = location.id;
let httpMethod = 'post';
if(id > 0 ) {
httpMethod = 'put';
url = `${API_URL}/api/locations/${id}`;
}
let options = {
method: httpMethod,
url: url,
headers: { 'Content-Type': 'application/json' },
data: location
}
return axios(options);
}
}
I would prefer if this were somehow made available to the global Vue instance and my components could access it somehow.
I believe this needs to be implemented in some form within my main.js file, possibly via Vue.use, but I'm still unsure of how to implement this even after reviewing the docs.
Any input would be greatly appreciated. Thanks!