vue cli 3.x 配置代理

1、根目录下新建vue.config.js文件夹
文件中需要设置远程代理;如下:

devServer: {
    overlay:{ // 关闭eslient 规则校验
      warning:false,
      errors:false
    },
    host: '127.0.0.1', // can be overwritten by process.env.HOST
    port: 8086,
    // proxy: {}
    proxy: {  // 如访问的接口地址是http://122.xxx.63.78:80/api/service
      '/api': {
        target: 'http://122.xxx.63.78:80', // 代理接口
        ws: false, // 是否启用websockets
        changeOrigin: true, // 开启代理
        secure: false, // 将安全设置为false,才能访问https开头的
        pathRewrite: {
          '^/api': '/api' // //这里理解成用‘/api’代替target里面的地址
        }
      }
    }
  },

2、在package.json文件中找到
script:如下

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
  },

增加srcipt中如下:

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start": "node index.js",
    "server": "nodemon index.js --ignore client"
  },

3、非常重要:一定要重启服务、一定要重启服务、一定要重启服务;
更多内容:可以查看github

发布了21 篇原创文章 · 获赞 0 · 访问量 2845

猜你喜欢

转载自blog.csdn.net/weixin_39593730/article/details/103364630