Switch Vue Proxy Without Restart

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': ''
        }
      }
}

 

评论

暂无评论。

登录后可发表评论。