当前位置:网站首页>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
边栏推荐
猜你喜欢
SVN详细使用教程
LeetCode151-颠倒字符串中的单词-字符串-模拟
Share 20 tips for ES6 that should not be missed
We reference My97DatePicker to realize the use of time plug-in
Arduino for esp8266串口功能简介
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
线程同步、生命周期
三、梯度下降求解最小θ
随机推荐
Alexnet model
【thymeleaf】处理空值和使用安全操作符
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
select 同时接收普通数据 和 带外数据
线程同步、生命周期
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Introduction to distributed transaction Seata
win10 任务栏通知区图标不见了
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
LeetCode167-两数之和II-双指针-二分-数组-查找
8.2 text preprocessing
Introduction to dirty reading, unrepeatable reading and phantom reading
8.3 language model and data set
2-GO variable operation
we引用My97DatePicker 实现时间插件使用
Detailed analysis of SQL combat of Niuke database (26-30)
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Programming philosophy - automatic loading, dependency injection and control inversion
Thread synchronization, life cycle