Vite生成的项目不太一样

在vite.config.js中设置

  1. import { resolve } from "path";
  2. function pathResolve(dir) {
  3. return resolve(__dirname, ".", dir);
  4. }
  5. module.exports = {
  6. alias: {
  7. "/@/": pathResolve("src"),
  8. },
  9. optimizeDeps: {
  10. include: ["@ant-design/icons-vue"],
  11. },
  12. proxy: {
  13. '/api': {
  14. target: 'http://192.168.199.9:8000',
  15. changeOrigin: true,
  16. rewrite: path => path.replace(/^\/api/, "")
  17. }
  18. }
  19. };

主要是下面这段,

proxy: {
‘/api’: {
target: ‘http://192.168.199.9:8000‘,
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, “”)
}
}

配置axios

  1. axios.defaults.baseURL = '/api'
  2. axios.defaults.withCredentials = true

使用axios

  1. setup(){
  2. onMounted(() => {
  3. axios
  4. .get("/some_url")
  5. .then((res) => {
  6. console.log(res);
  7. })
  8. .catch((err) => {
  9. console.log(err);
  10. });
  11. });
  12. }

请求 /api/some_url 被代理到:http://192.168.199.9:8000/some_url