In iview, how to configure and encapsulate axios, and how to call it?

I don"t quite understand the default util.js configuration. I just got in touch with iview.

< hr >

const ajaxUrl = "http://192.168.1.230:8080/nis-service/"
the ajax prefix
util.ajax = axios.create ({baseURL: ajaxUrl,timeout: 30000}) is configured here, and
axios is configured here, how to call it?
now I am directly modifying the vue prototype chain. I feel that this is not the original intention of the iview author. I want to know how to call it.
axios.defaults.baseURL = ajaxUrl;
Vue.prototype.$ajax = axios;

< hr >

and how to encapsulate it in the util.js file?

Mar.03,2021

after groping for a few days, I basically know how to use it, and I throw a brick to attract jade here.

configure ajaxUrl, prefix address

const ajaxUrl = env === 'development' ?
    'http://192.168.1.230:8080/nis-service/' ://
    env === 'production' ?
    'https://www.url.com' ://
    'https://debug.url.com';//debug

ternary selector, configured in build/env.js

export default "development";//developmentproduction

configure parameters in util.ajax

util.ajax = axios.create({
    baseURL: ajaxUrl,//
    timeout: 30000,//
    headers: {//
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
    }
});

introduce qs into the util.js file

import qs from 'qs';

create your own method in util.js, take post as an example

util.post = function (url, data) {
    return util.ajax({
        url,//
        method: 'post',//
        data:qs.stringify(data,{ arrayFormat: "repeat" })//qs
    })
    .then(response => {//
        console.log(response);
        return response;
    })
    .catch(error => {//
        console.log(error);
        return error;
    });
}

introduce util.js in the components you need to use.

import util from "../../../libs/util.js";

just use it directly

util.post("url",{
  key;value
})
.then(response => {
})
.catch(error => {
});

if you have a better package or use, please do not hesitate to give us your advice, and please pat the passing god.


good, learn

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36f24-3441f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36f24-3441f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?