Vue3 axios跨域配置

安装axios

npm install axios vue-axios

main.js里

import axios from 'axios'
import VueAxios from 'vue-axios'
 
const app = createApp(App) // 创建实例
 
app.config.globalProperties.$axios = axios
axios.defaults.baseURL = '/aposServer'
axios.defaults.headers.post['Content-Type'] = 'application/json';

app.config.productionTip = false
app.use(VueAxios,axios);

在package.json同级目录下加个vue.config.js文件

module.exports = {
  devServer: {
    proxy: {
      '/aposServer': {
          target: 'https://localhost:44310/api', //API服务器的地址
          changeOrigin: true,
          pathRewrite: {
              '/aposServer': ''
          }
      }
  },
  }
}

调用webapi

this.$axios.post("/Equipments/GetAllEquipmentsTest")
.then(res=>{
   console.log(res)
})
.catch(err=>{
   console.log(err)
})
            }

重启下服务

 

posted @ 2021-03-04 15:21  liuyong111  阅读(732)  评论(0编辑  收藏  举报