vue2设置跨域代理

268 阅读1分钟

vue.config.js

module.export: {
    devServer: {
        host: '0.0.0.0',
        https: false,
        hotOnly: false,
        proxy: {
            '/proxy': {
                target: 'http://47.107.96.218:3204/admin/api/',
                // 允许跨域
                changeOrigin: true,
                pathRewrite: {
                    '^/proxy': ''
                }
            }
        }
    }
}

http.js

//只需要设置
import Vue from 'vue'
import axios from 'axios'

const http = axios.create({
       baseURL: '/proxy'
})
 
export {
      http
}

main.js

import { http } from '@/http'
Vue.prototype.$http = http

//在其它组件页面就可以跨域请求数据
this.$http.get('url')