如何配置跨域,代理域名,下面是vite的代理
server: {
port: 8516,
host: true,
open: true,
proxy: {
'/license-province': {
target: 'http://xxx.xxx.x.xxx:xxxx',
changeOrigin: true,//是否跨域
rewrite: (p) => p.replace(/^\/license-province/, 'license-province')//重寫路徑
}
}
},
區(qū)分開發(fā)環(huán)境和生產(chǎn)環(huán)境,以及預(yù)發(fā)布環(huán)境
在根目錄創(chuàng)建 .env[mode]文件,在項(xiàng)目執(zhí)行 npm run dev 的時候vite會自動去讀取 .env.development 文件里面的配置,執(zhí)行 npm run build 進(jìn)行打包之后也會自動將 .env.production 的內(nèi)容打包進(jìn)去.
注意: 如果你想進(jìn)入預(yù)發(fā)布模式的話需要在打包的時候進(jìn)行mode配置: npm run build --mode staging
公共的: .env
開發(fā)環(huán)境: .env.development
生產(chǎn)環(huán)境: .env.production
預(yù)發(fā)布環(huán)境: .env.staging

我們的 .env.development 和 .env.production 文件里面都會有 VITE_APP_ENV 配置:


在我們的 vite.config.js文件中:

以上是 vite.config.js 的配置,上面展示了在不同環(huán)境下去請求對應(yīng)環(huán)境的域名并且配置代理進(jìn)行跨域.文章來源:http://www.zghlxwxcb.cn/news/detail-558220.html
VUE中常用proxy來解決跨域問題
1.在vue.config.js中設(shè)置一下代碼:
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: { // 配置跨域
'/api':{
target:`http://xxx.xxx.xxx`, //請求后臺接口
changeOrigin:true, // 允許跨域
pathRewrite:{
'^/api' : '' // 重寫請求
}
}
},
}
2. 創(chuàng)建axioss實(shí)例時,將baseUrl設(shè)置為 '/api'文章來源地址http://www.zghlxwxcb.cn/news/detail-558220.html
const http = axios.create({
timeout: 1000 * 1000000,
withCredentials: true,
BASE_URL: '/api'
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
到了這里,關(guān)于vue中vite.config.js配置跨域以及環(huán)境配置詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!