Switch Vue Proxy Without Restart
发布于 2026-07-06 02:58:49
1 次浏览
config.json
{ "api": "http://localhost:9001" }
// vue.config.js
const hotRequire = modulePath => {
// require.resolve can get absolute path from relative path
// Delete the cached file in require using absolute path as key
delete require.cache[require.resolve(modulePath)]
// Re-get the file content
const target = require(modulePath)
return target
}
...
proxy: {
'/api': {
// If router is valid, prefer the value returned by router
target: 'that must have a empty placeholder',
changeOrigin: true,
// Execute the router function on every HTTP request
router: () => (hotRequire('./src/utils/config') || {}).api || '',
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
暂无评论。