当前位置:网站首页>umi 如何设置环境变量并使用
umi 如何设置环境变量并使用
2022-08-06 00:38:00 【hzxOnlineOk】
首先,安排上umi的官方文档:
https://v3.umijs.org/zh-CN/docs/config
接下来,实战演练,分别在根目录下创建 .umirc.ts 以及 .umirc.prod.ts文件(js文件同理)
为了兼容mac的环境变量,我们需要使用cross-env这个npm包进行环境变量设置
npm i cross-env -S
创建后的2个文件填充以下代码:
.umirc.ts (默认作为dev环境)
import {
defineConfig } from 'umi';
// let target = 'http://11.2.33.222:8080';
let target = 'https://xxxx.xxxx.xxxx.cn';
export default defineConfig({
nodeModulesTransform: {
type: 'none',
},
routes: [
{
path: '/', component: '@/pages/index' },
{
path: '/AssetsTypeList', component: '@/pages/AssetsTypeList' },
],
define: {
// 重点就是这个属性了,设置全局变量
// 在项目中,可以通过process.env.NODE_ENV 或者
// process.env.UMI_ENV 或者process.env.date得到对应环境的值
'process.env': {
NODE_ENV: 'dev',
UMI_ENV: 'dev',
date: '2022-08-01',
},
},
fastRefresh: {
},
// mfsu: {},
alias: {
'@': '/src',
},
hash: true,
proxy: {
'/govern': {
target,
changeOrigin: true,
},
'/label': {
target,
changeOrigin: true,
},
},
});
.umirc.prod.ts
import {
defineConfig } from 'umi';
export default defineConfig({
define: {
'process.env': {
NODE_ENV: 'prod',
UMI_ENV: 'prod',
date: '2022-08-22',
},
},
});
接着,我们需要改一下package.json的script中的启动和打包命令
package.json
{
"private": true,
"scripts": {
"start": "umi dev", // 由于我们默认本地是dev 因此不用配置,
// 如果想区分local和dev,则需要在根目录再创建一个.umirc.dev.ts文件
"build": "cross-env UMI_ENV=prod umi build", // 重点在这里!!!添加cross-env UMI_ENV=prod
"postinstall": "umi generate tmp",
"prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",
"test": "umi-test",
"test:coverage": "umi-test --coverage"
},
"gitHooks": {
"pre-commit": "lint-staged"
},
"lint-staged": {
"*.{js,jsx,less,md,json}": [
"prettier --write"
],
"*.ts?(x)": [
"prettier --parser=typescript --write"
]
},
"dependencies": {
"@ant-design/pro-layout": "^6.5.0",
"@antv/g2plot": "^2.4.20",
"@antv/g6": "^4.6.15",
"antd": "^4.21.7",
"cross-env": "^7.0.3",
"echarts": "^5.3.3",
"moment": "^2.29.4",
"react": "17.x",
"react-dom": "17.x",
"umi": "^3.5.30"
},
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@umijs/preset-react": "1.x",
"@umijs/test": "^3.5.30",
"lint-staged": "^10.0.7",
"prettier": "^2.2.0",
"typescript": "^4.1.2",
"yorkie": "^2.0.0"
}
}
然后在任意的组件中,打印使用全局变量
console.log('环境变量:', process.env, process.env.UMI_ENV);
dev:然后我们在本地构建完成,打开F12查看是否打印出全局变量(dev环境,可以理解就是本地环境local)
prod:生产环境则需要先打包部署,之后打开F12查看是否打印出全局变量
边栏推荐
- What is a man-in-the-middle attack?
- Go 语言 strings 库常用方法
- 区间DP
- FPGA IIC SLAVE 实现
- Privacy Computing Basic Component Series - Obfuscation Circuit
- window.postMessage() Cross-domain communication between parent and child pages
- 【LeetCode】209. 长度最小的子数组
- Day4:面试必考选择题
- el-table的暂无数据怎么变成图片加文字按钮?
- 2022 China Health Exhibition, Shandong Health Exhibition, Jinan Health Exhibition, Health Industry Exhibition
猜你喜欢

【电商运营】网上商店如何利用好自己的营销数据达成目标?
![[kali-vulnerability exploitation] (3.2) Metasploit basics (middle): Armitage tool exploitation process](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[kali-vulnerability exploitation] (3.2) Metasploit basics (middle): Armitage tool exploitation process

sqli-labs(安装与配置)报错原因

【TA-霜狼_may-《百人计划》】图形2.7.2 GPU硬件架构概述

为啥强烈禁止使用Calendar?

Vernacular Machine Learning - Convolutional Neural Network CNN
Taught you how to locate online MySQL lock timeout problem, BaoJiao package

MySQL:索引的基础知识

网页使用微信扫码登录

Qt入门(零)——Qt概述
随机推荐
MySQL:存储引擎
wsa无法安装如何解决
Solve the problems arising from the mysql statement MAX () function
Day3:面试必考选择题
Facial Expression Recognition---Study Notes
【TA-霜狼_may-《百人计划》】图形2.7.2 GPU硬件架构概述
Oracle学习笔记组合索引(十二)
ERP仓库管理系统查询(十)
数据库应用系统中有什么核心问题
我明明加了唯一索引,数据库怎么还是产生重复数据?
About the time format and the method of getting the specified time
One-Class Convolutional Neural Network
组织结构图OrgChart.js
Viola-Jones Detector (VJ)---Study Notes
Convert the children array to a one-bit array
k线图怎么看止损位置
Vernacular Machine Learning - Convolutional Neural Network CNN
window.postMessage() Cross-domain communication between parent and child pages
Use WeChat to scan the code to log in
Certificate Manager in Kubernetes - cert-manager