当前位置:网站首页>Nuxt project: Global get process Env information
Nuxt project: Global get process Env information
2022-04-23 14:56:00 【snowball@li】
One 、 project :nuxt project
Two 、 demand :
Environment variable configuration
At the start of the project Or after packing , Get a global variable information , So we can do something .
3、 ... and 、 What information does an environment variable usually contain :
NODE_ENV: Defined in the project scaffold , Tell us it's a development environment (development) Or online environment (production)
PROJECT_NAME: Project name , Tell us what the current project is . In large projects with multiple projects, it is very important .
API_SERVER: The service to which the interface address belongs .
MODE: The current project environment (dev、test、uat、pre、prd)
Four 、 How to define 、 Using environment variables :
4.1、 stay .env The document defines
4.2、 stay package.json file scripts To define :
// What is defined here MODE、PRO Are defined environment variables
"scripts": {
"dev": "cross-env MODE=DEMO PRO=CHAAA nuxt",
},
4.3、 stay nuxt.config.js file env Configuration in :
// .env All variables defined inside can be defined here
// Empathy package.json The defined in can also be configured here
env: {
COOKIE_PATH: process.env.COOKIE_PATH || '/',
PUBLIC_PATH: process.env.PUBLIC_PATH || '/',
SERVER_NAME: process.env.PUBLIC_PATH || '/',
API_SERVER: process.env.API_SERVER || '/',
MODE: process.env.MODE || '/',
PUBLIC_TEST: process.env.PUBLIC_TEST || '/',
},
4.2.3、 Use
// Find one js file , After re project , Print information
console.log('49env-DEVELOPMENT-PRODUCTION', process.env.NODE_ENV)
console.log('50env', process.env.PUBLIC_PATH)
console.log('51env', process.env.SERVER_NAME)
console.log('52env', process.env.API_SERVER)
console.log('53env', process.env.API_SERVER_Z)
console.log('54env', process.env.MODE)
console.log('55env', process.env.PRO)
console.log('56env', process.env.PUBLIC_TEST)
The browser prints the corresponding information :
5、 ... and 、 elementary analysis
process Objects are global variables , It provides the current node.js about , And control the current node.js Relevant processes of . Because it's a global variable , For it node Applications are always available , There is no need to require().
since process It's all one object ,env Nature is one of its properties , This property returns the object containing the user's environment information . Input... At the terminal node after , In the input process.env You can see the printed information .
process.env
yes node Environment variables in , You can't get it directly in the module , such as .js
、.vue
, You need to configure DefinePlugin.
DefinePlugin You can define some global variables , Let's use... Directly in the module , Don't make any statements .
Please note that ,Nuxt Use webpack Of definePlugin To define environment variables . It means Node.js Medium process
or process.env
Neither available nor defined .nuxt.config.js Each of the env Attributes are individually mapped to process.env.xxxx
And perform conversion during compilation .
intend ,console.log(process.env)
Will output {}
, but console.log(process.env.you_var)
Your value will still be output . It will process.env.your_var
Replace all instances of with the value you set to . namely :env.test ='testing123'
. If you use... Somewhere in the code process.env.test
, It is actually translated into 'testing123'.
6、 ... and 、 Reference link :
vue.js Medium process.env - You know
node The production of env How to inject files _process.env.NODE_ENV Related issues _weixin_39641334 The blog of -CSDN Blog node The production of env How to inject files _Vue Function process.env.NODE_ENV_weixin_39681621 The blog of -CSDN Blog
API: Environment variable configuration - NuxtJS | Nuxt.js Chinese net
版权声明
本文为[snowball@li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231452566472.html
边栏推荐
- epoll 的 ET,LT工作模式———实例程序
- I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
- go基础 反射
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
- Introduction to Arduino for esp8266 serial port function
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
- 解决computed属性与input的blur事件冲突问题
- Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
- LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
- Alexnet model
猜你喜欢
capacitance
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
UML学习_day2
win10 任务栏通知区图标不见了
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
分享 20 个不容错过的 ES6 的技巧
自动化的艺术
1 - first knowledge of go language
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
随机推荐
Leetcode149 - maximum number of points on a line - Math - hash table
[untitled]
How do I open the win10 startup folder?
LeetCode162-寻找峰值-二分-数组
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
帧同步 实现
Introduction to distributed transaction Seata
A good tool: aardio
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
牛客网数据库SQL实战详细剖析(26-30)
Is asemi ultrafast recovery diode interchangeable with Schottky diode
eolink 如何助力遠程辦公
Leetcode165 compare version number double pointer string
Leetcode151 - invert words in string - String - simulation
OPPO数据湖统一存储技术实践
1 - first knowledge of go language
epoll 的EPOLLONESHOT 事件———实例程序
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
PCIe X1 插槽的主要用途是什么?
I/O复用的高级应用:同时处理 TCP 和 UDP 服务