vue3.0配置代理proxy 主要解決的是接口問題和路徑問題。
這里說的是vue3.0的版本:
1.首先在項目的根目錄下添加vue.config.js文件。
2.然后 vue.config.js內容如下:
module.exports = {
// 基本路徑
publicPath: './',
// 輸出路徑
outputDir: 'dist',
// 靜態資源
assetsDir: './',
// eslint-loader是否在保存時候檢查
lintOnSave: true,
// 服務項配置
devServer: {
host: 'localhost',
port: 8080,
https: false,
open: true,
// 設置代理proxy
proxy: {
'/api':{
'target':‘
http://localhost:3000',
changeOrigin:true, //表示是否跨域,
pathRewrite:{ //表示需要rewrite重寫的
'^/api':' ',
}
}
}
}
}
3.最后請求的時候:
譬如:"http://localhost:3000/login"
現在寫成:"/api/login".
用 /api 替換原來的 http://localhost:3000