vue 跨域 怎么办?
发布日期:2021-05-07 16:06:19 浏览次数:19 分类:精选文章

本文共 2662 字,大约阅读时间需要 8 分钟。

Vue ????????

???????????????????????????? Vue ????????????????????????????????????????????????????? Vue ??? vue.config.js ????????

1. ????

?????????????????????????????????????????????????????????????????????????????????????????????

2. Vue ???????

Vue ???????????????????????????????????????????????????????????? 404?????????

3. ?? Vue ???????

??????????????? vue.config.js ???????????????????

  • ????? vue.config.js ??????????????? vue.config.js ????????????????????????

  • ?? devServer ???? vue.config.js ???? devServer ????????

    module.exports = {  devServer: {    open: true, // ?????    host: 'localhost', // ??????    port: 8080, // ?????    https: false, // ?? HTTPS ??????    proxy: {      '/api': {        target: 'http://localhost:8888/api/', // ?????????????        ws: true, // ?? WebSocket ??        changeOrigin: true, // ????        pathRewrite: {          '^/api': '' // ???? /api        }      },      '/dwp': {        target: 'http://localhost:8881/dwp/', // ?????????????        ws: true,        changeOrigin: true,        pathRewrite: {          '^/dwp': ''        }      }    }  }}

    ??????/api ? /dwp ???????????????????pathRewrite ????????????????????????????????

  • ??????????????? npm run dev ? yarn dev?Vue ??????? vue.config.js?????????? devServer?

  • ????????????? Axios ? native fetch ???????????? /api ? /dwp???????? URL????

    import axios from 'axios';Vue.prototype.$http = axios;// ?const fetch = (url, options) => {  return new Promise((resolve, reject) => {    fetch(url, options).then(response => {      resolve(response);    }).catch(error => {      reject(error);    });  });};

    ???????????????????????????

  • 4. ??????

    ??????????????????????????????? vue.config.js ?????????????????

    module.exports = {  devServer: {    open: true,    host: 'localhost',    port: 8080,    https: false,    proxy: {      '/api': {        target: 'http://localhost:8888/api/',        ws: true,        changeOrigin: true,        pathRewrite: {          '^/api': ''        }      },      '/dwp': {        target: 'http://localhost:8881/dwp/',        ws: true,        changeOrigin: true,        pathRewrite: {          '^/dwp': ''        }      },      '/admin': {        target: 'http://localhost:8882/admin/',        ws: true,        changeOrigin: true,        pathRewrite: {          '^/admin': ''        }      }    }  }}

    ???????????????????????????????????

    5. ????

    • ??????????????????????????????????http://localhost:8888 ??????? /api ??????

    • ??????????????????????? pathRewrite?????????????

    • ?????????????????????????????????????????????????

    6. ??

    ???? Vue ? vue.config.js?????????????????????????????????????????????????????????????????????????????????????????????

    上一篇:git-修改忽略文件
    下一篇:web浏览器数据本地存储

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2025年05月08日 23时13分56秒