Uniapp’s method of determining the running environment: You can use [process.env.NODE_ENV] to determine whether the current environment is a development environment or a production environment. The code is [if(process.env.NODE_ENV === 'development'){ console.log].
The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, thinkpad t480 computer.
Recommendation (free): uni-app development tutorial
How uniapp determines the running environment:
uni-app
You can use process.env.NODE_ENV
to determine whether the current environment is a development environment or a production environment. Generally used to dynamically switch between connecting to a test server or a production server.
In HBuilderX, the code compiled by clicking "Run" is the development environment, and the code compiled by clicking "Release" is the production environment
cli mode is the common compilation environment processing method.
if(process.env.NODE_ENV === 'development'){ console.log('开发环境') }else{ console.log('生产环境') }
If you need to customize more environments, such as test environments:
Assuming that you only need to configure a single platform, you can package.json In the configuration, there will be one more in the run and release menu of HBuilderX.
If it is configured for all platforms, it can be configured in vue-config.js.
Quick code block
Type the code blocks uEnvDev and uEnvProd into HBuilderX to quickly generate the running environment determination code corresponding to development and production.
// uEnvDev if (process.env.NODE_ENV === 'development') { // TODO } // uEnvProd if (process.env.NODE_ENV === 'production') { // TODO }
Related free learning recommendations: Programming Video
The above is the detailed content of How does uniapp determine the operating environment?. For more information, please follow other related articles on the PHP Chinese website!