I'm grappling with obtaining a JSON file from the server. The endpoint requires a year parameter, which needs to be set as the current year number as its value (e.g., ?year=2019). Furthermore, I need to fetch data for the previous and upcoming years as well. However, hardcoding the years isn't a viable solution since it will require constant manual updates.
Even though my current setup is pretty basic, I've experimented with various approaches that haven't yielded the desired results.
data() {
return {
year:[]
}
},
computed: {
axiosParams(){
const params = new URLSearchParams();
params.append('year', this.year);
return params;
}
},
getYears: function() {
axios.get('myurl',{
params : this.axiosParams
}
}).then((response) => {
this.year = response.data;
})
}
When I hardcoded the year by setting it as '2019' in the data section, everything functioned correctly. As a newcomer to Vue and Axios, I'd greatly appreciate any assistance you can provide.