Best Way to Pass Query Parameters to URL Using Axios in Vue? Best Way to Pass Query Parameters to URL Using Axios in Vue? vue.js vue.js

Best Way to Pass Query Parameters to URL Using Axios in Vue?


You can use the second argument to .get if you want to pass an object instead:

axios.get(this.jobsListURL, {  params: this.axiosParams})

This is pretty much the same, but you don't have to do the string manipulation for the URL.

You can build that object like this:

computed: {    axiosParams() {        const params = new URLSearchParams();        params.append('param1', 'value1');        params.append('param2', 'value2');        return params;    }}


if this.AxiosParams is a json object like { "posttype": "new" }, this will work:

axios.get(this.jobsListURL, this.AxiosParams)

But this doesn't work when the values are arrays, then you could add a paramsSerializer.

//import qs from 'qs'; (https://www.npmjs.com/package/qs)this.http = axios.create({    baseURL: `https://mydomain/api/v1/`,    paramsSerializer: (params) => {        return qs.stringify(params, {arrayFormat: 'repeat'});    },});this.http.get('jobs', this.AxiosParams);