Home > Web Front-end > uni-app > body text

How does uniapp determine the operating environment?

coldplay.xixi
Release: 2023-01-13 00:44:09
Original
8835 people have browsed it

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].

How does uniapp determine the operating environment?

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('生产环境')
}
Copy after login

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
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!