Axios call interface in vue2.0

Installation: npm install axios --save
Globally mount in the main.js entry file (here, use the prototype chain to mount):

import axios from 'axios';
Vue.prototype.$http = axios;

The index.js under the config folder configures the server environment

module.exports = {
    
    
  dev: {
    
    //开发服务器
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
    
    //代理跨域
      '/equipment/':{
    
    //公共前缀
        target:'接口的公共域名部分',
        changeOrigin:true//跨域
      }
    host: 'localhost',
    port: 8080, //端口号可更改
    autoOpenBrowser: false,//默认不自行打开浏览器
    errorOverlay: true,//查询错误
    notifyOnErrors: true,//通知错误
    poll: false, //轮询监控
    devtool: 'cheap-module-eval-source-map',//webpack提供的用来方便调试的配置
    cacheBusting: true,//缓存破解
    cssSourceMap: true//是否开启cssSourceMap
  },
  build: {
    
    
	index: path.resolve(__dirname, '../dist/index.html'),//编译后index.html的路径
    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),//打包后的文件根路径
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',//静态资源的公开路径,即真正的引用路径
    productionSourceMap: true,
    devtool: '#source-map',
    productionGzip: false,//生产环境压缩代码
    productionGzipExtensions: ['js', 'css'],//定义压缩文件
    bundleAnalyzerReport: process.env.npm_config_report//是否开启打包后的分析报告
  }
}

Apply in the vue page:

this.$http.post('接口后缀名',{
    
    params}).then(res=>{
    
    
		console.log(res)
	}).catch(err=>{
    
    
    	console.log(err)
   })
})

Guess you like

Origin blog.csdn.net/weixin_46021808/article/details/109359092