vue-cli中axios封装

1.下载依赖

cnpm i axios --save

2.在src下创建api文件夹,在api文件夹下创建http.js(前提安装mint-ui)

import axios from 'axios'
const { Indicator } =require('mint-ui')
// axios.defaults.baseURL = 'http://139.196.20.27:8882/appBiz'
axios.defaults.baseURL = 'http://www.luweicheng.com:8882/njBiz'
// axios.defaults.baseURL = 'http://192.168.4.121:80'

axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

export default {
  install: function (Vue) {
    Vue.prototype.$post = params => {
	  Indicator.open("加载中...");
      return new Promise((resolve, reject) => {
        axios.post("/CallMethod", params)
          .then(response => {
			Indicator.close()
            resolve(response.data);
          })
          .catch(err => {
			Indicator.close()
            reject(err);
          })
      })
    }
  }
}

3.main.js

import http from './api/http'
Vue.use(http)

 

你可能感兴趣的:(web前端)