Vue-cli 3.0无配置文件以及跨域问题

Prototype

Vue.prototype.$axios = axios    //把axios挂载到vue的原型中,在vue中每个组件都可以使用axios发送请求
Vue.prototype.HOME = '/api'    //重要在于这里,Vue.prototype.HOME = '/api'是一个定值,默认指向localhost,所有修改指向路径为'/api',配置文件index.js定义的可跨域路径

官方文档:

https://cli.vuejs.org/zh/config/#%E5%85%A8%E5%B1%80-cli-%E9%85%8D%E7%BD%AE

配置vue.config.js文件

根目录下创建即可
按照需要添加配置信息

// vue.config.js
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: './public/api',
        ws: true,
        changeOrigin: true
      },
      '/oapi': {
        target: 'http://localhost:80',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/oapi': 'http://localhost:80/api', // rewrite path
        }
      }
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

BACK TO TOP