假設 mock 接口為http://192.168.1.1:9000/1
module.exports = {
devServer: {
// overlay: { // 讓瀏覽器 overlay 同時顯示警告和錯誤
// warnings: true,
// errors: true
// },
// open: false, // 是否打開瀏覽器
// host: "localhost",
// port: "8080", // 代理斷就
// https: false,
// hotOnly: false, // 熱更新
proxy: {
"/api": {
target: "http://192.168.1.1:9000", // 目標代理接口地址
secure: false,
changeOrigin: true, // 開啟代理,在本地創(chuàng)建一個虛擬服務端
// ws: true, // 是否啟用websockets
pathRewrite: {
"^/api": "/"
}
}
}
}
};
訪問:
<script>
import axios from "axios";
export default {
mounted() {
axios.get("/api/1").then(res => {
console.log('proxy:', res);
});
}
};
</script>