Vite生成的项目不太一样
在vite.config.js中设置
import { resolve } from "path";
function pathResolve(dir) {
return resolve(__dirname, ".", dir);
}
module.exports = {
alias: {
"/@/": pathResolve("src"),
},
optimizeDeps: {
include: ["@ant-design/icons-vue"],
},
proxy: {
'/api': {
target: 'http://192.168.199.9:8000',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, "")
}
}
};
主要是下面这段,
proxy: {
‘/api’: {
target: ‘http://192.168.199.9:8000‘,
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, “”)
}
}
配置axios
axios.defaults.baseURL = '/api'
axios.defaults.withCredentials = true
使用axios
setup(){
onMounted(() => {
axios
.get("/some_url")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
});
}
请求 /api/some_url 被代理到:http://192.168.199.9:8000/some_url