vue webpack打包后动态修改后台接口的配置

1.在static文件下添加setup.js
在这里插入图片描述
在里面添加ip, window.ip是添加全局变量

window.ip={
	baseurl:'http://localhost:8888'//  自己的ip地址
}

2.将setup.js文件引入index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
	<script type='text/javascript' src="./static/setup.js"></script>   //如果写到body下可能获取不到
   <title>管理系统</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
 <style>
  </style>
</html>

3.在自己配置的全局接口中引入

baseURL: window.ip.baseurl,
//可以在全局调用 window.ip.baseurl

4.然后就npm run build打包,打包后在dist/static文件下会出来一个setup.js
在这里修改ip,然后进行刷新就可以啦
在这里插入图片描述

window.ip={
	baseurl:'http://localhost:6666'  //修改ip
}
posted @ 2020-12-28 11:46  倔强的烤马铃薯  阅读(23)  评论(0)    收藏  举报  来源