前端解决跨域(亲测有效)-vue

  1. 在根目录中创建文件vue.config.js
  2. 在vue.config.js文件中添加以下代码
    module.exports = {
    publicPath: './',
    outputDir: 'dist',
    assetsDir: 'static',
    lintOnSave: false, //是否开启eslint
    devServer: {
    open: true, //是否自动弹出浏览器页面
    host: "localhost",
    port: '8081',
    https: false, //是否使用https协议
    hotOnly: false, //是否开启热更新
    proxy: {
    '/api': {
    target: '此处填写后端给你的服务器地址',
    ws: true, //代理websockets
    changeOrigin: true, // 虚拟的站点需要更管origin
    pathRewrite: { //重写路径 比如'/api/aaa/ccc'重写为'/aaa/ccc'
    '^/api': ''
    }
    }
    }
    }
    }
  3. 引用示例(就是用/api替换原来的服务器地址)
    export function Login(params) {
    return req.post("/api/login", params)
    }
posted @ 2020-11-02 14:54  王较瘦u  阅读(252)  评论(0)    收藏  举报