First of all inpackage.jsonDefine environment in

{
    "uni-app": {
        "scripts": {
            "test": {
                "title": "H5 Test Environment"."env": {
                    "UNI_PLATFORM": "h5"."APP_ENV": "H5-Test"}},"pro": {
                "title": "H5 Formal Environment"."env": {
                    "UNI_PLATFORM": "h5"."APP_ENV": "H5-Pro"
                }
            }
        }
    }
}
Copy the code

Then place one in the root directory.envfolder

/.env/index.js

let ENV_VAR = null

if (process.env.NODE_ENV === 'development') {  
  ENV_VAR = require('./.env.dev.js').default
} else {
  if (process.env.APP_ENV == "H5-Test") {
    ENV_VAR = require('./.env.dev.js').default
  } else if (process.env.APP_ENV == "H5-Pro") {
    ENV_VAR = require('./.env.prod.js').default
  }
}

if (ENV_VAR) {
  process.uniEnv = {}
  for (let key in ENV_VAR) {  
    process.uniEnv[key] = ENV_VAR[key]
  }  
}
Copy the code

/.env/.env.dev.js

export default {
  // config...
}
Copy the code

/.env/.env.prod.js

export default {
  // config...
}
Copy the code

Finally, inmain.jsIn the injection

import './.env'
Copy the code

use

process.uniEnv.config
Copy the code