vant/vue跨域请求解决方案

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vant/vue跨域请求解决方案

Nanchen_42   2022-12-24 我要评论

因为后端给的接口不是本地的接口,所以需要跨域来获取接口数据

在vue.config.js中对其进行配置

devServer: {
    host: 'localhost',
    port: 3030, // 端口
    https: false,
    proxy: {
        // 配置跨域
        '/api': {
            target: 'http://xx.xx.xx.xxx', // 代理的接口域名以及端口号;
            ws: true, // 支持ws协议;websocket的缩写;
            changeOrigin: true, // 是否跨域
            pathRewrite: { // 路径替换
                '^/api': '/api'
            }
        }
    },
    hotOnly: false,
    before: app => {}
},

在axios.js中配置baseURL

let config = {
  baseURL:'/api'
};

最后使用$axios来请求数据即可

mounted(){
    this.$axios.get('index_category/data').then((v)=>{
      console.log(v);
    })
  }

成功后效果如下 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们