vue2.0 中axios 调用接口

安装:npm install axios --save
main.js 入口文件中全局挂载(这里使用原型链挂载):

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

config文件夹下的index.js 配置服务器环境

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//是否开启打包后的分析报告
  }
}

在vue页面中应用:

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

猜你喜欢

转载自blog.csdn.net/weixin_46021808/article/details/109359092