vue切换proxy不重启
首页 > >    作者:lininn   2023年2月13日 15:12 星期一   热度:573°   百度已收录  
时间:2023-2-13 15:12   热度:573° 
config.json

{ "api": "http://localhost:9001" }
// vue.config.js
const hotRequire = modulePath => {
  // require.resolve可以通过相对路径获取绝对路径
  // 以绝对路径为键值删除require中的对应文件的缓存
  delete require.cache[require.resolve(modulePath)]
  // 重新获取文件内容
  const target = require(modulePath)
  return target
}

...
proxy: {
      '/api': {
        // 如果router有效优先取router返回的值
        target: 'that must have a empty placeholder',
        changeOrigin: true,
        // 每次发起http请求都会执行router函数
        router: () => (hotRequire('./src/utils/config') || {}).api || '',
        ws: true,
        pathRewrite: {
          '^/api': ''
        }
      }
}

 
二维码加载中...
本文作者:lininn      文章标题: vue切换proxy不重启
本文地址:?post=572
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: lininn