2021-10-08--create-react-app--打包后修改配置

277 阅读1分钟

最近在开发过程中遇到一个问题,打包后需要部署在两套环境下,一套是公司局域网环境,另一套是正式生产环境,但是在代码中涉及到跳转到其他系统,公司环境和线上环境不一致,因此需要打包两次,打包时间有点长,因此想用一个公共配置文件,打包后修改一下配置即可,不用打包两次

1.创建配置文件config.js,放在public目录下,public文件会被拷贝到dist目录下

const urls = {
	currentUse: "ipv6URL",
	urls: {
		ipv4URL: {
			back: "http://192.168.140.2:3000/",
			home: "http://192.168.140.2:3300/select"
		},
		ipv6URL: {
			back: "./admin/",
			home: "/select/"
		}
	}
}

###2.index.html引入

  <script src='./config.js'></script>

###3.组件中使用配置


  goBack = () => {
    this.setState({
      reportWarningVisible: false
    })
    console.log(urls)
    window.location.href = urls.urls[urls.currentUse].back
    // window.location.href = './admin/'

  }

  goMain = () => {
    this.setState({

      reportWarningVisible: false
    })
    console.log(urls)
    // window.location.href = '/select/'
    window.location.href = urls.urls[urls.currentUse].home
  }

4.tsx会报错找不到urls导致不通过,只需要定义一下 image.png

declare var urls: any