配置vue,config.js

  1. module.exports = {
  2. devServer: {
  3. open: true,
  4. host: 'localhost',
  5. port: 8080,
  6. https: false,
  7. //以上的ip和端口是我们本机的;下面为需要跨域的
  8. proxy: {//配置跨域
  9. '/kua': {
  10. target: 'https://mobile.iwangxing.cn/web/api',//这里后台的地址模拟的;应该填写你们真实的后台接口
  11. ws: true,
  12. changOrigin: true,//允许跨域
  13. pathRewrite: {
  14. '^/kua': ''//请求的时候使用这个api就可以
  15. }
  16. }
  17. }
  18. }
  19. }

挂载到vue对象

  1. Vue.prototype.$http = axios.create({
  2. //baseURL:process.env.VUE_APP_API_UR || '/web/api'
  3. //baseURL:'kua'
  4. })

使用时

  1. async fetch(){
  2. const res = await this.$http.get('kua/heroes/list')
  3. console.log(res);
  4. //this.newsCats = res.data
  5. }