preface
Environment variables: process.env.node_env
The process object is a global variable that provides information about the current Node.js and the process that controls it. Env is an attribute of Process.
NODE_ENV is not a native property of the process.env object; it is an environment variable we added ourselves to determine the current stage of development. The production stage is set to Production and the development stage is set to Develop.
Process.env.node_env is then read from the script.
Case 1 process.env.node_env in VUE-CLI3.0
Vue-cli3 uses DefinePlugin to help us configure process.env.node_env. In webpack global variables, vue-cli-service is good
Development: production: production: test: extension: cli3.0 root directory.env.env. development is the encapsulation entry that Vue adds to process.env (VUE_APP_)
In case 2, process.env.node_env needs to be added before VUe-cli3.0
You need to add commands to the scripts of the package.json file
NPM install –save-dev cross-env (don’t ask me why, Windows kernel is not compatible with Liux kernel
dev”: “cross-env NODE_ENV=development”, “build”: “cross-env NODE_ENV=production”,
After setting this, we can use process.env.node_env in scripts (which satisfies most requirements), but not in modules – DefinePlugin
DefinePlugin allows us to create global variables that can be set at compile time by adding the following code to webpack.config.js to configure global variable information module.exports = {plugins: [ new webpack.DefinePlugin({ ‘process.env’: { NODE_ENV: JSON.stringify(process.env.NODE_ENV) } }) ] }
The body of the
Export const queryProductList = params => fetch(API.QUERY_PRODUCT_LIST, params, ‘get’, ‘mock’)
2. The API encapsulation layer replaces the interface address with the mock identifier (mock) and environment identifier (rocess.env.node_env).
If (mock && process.env.node_env === 'testing') {} url = modifyUrl('966', extractUrl[1]) Passes api-mock's project identifier, interface pathCopy the code
Note: When there are multiple services in a project, be careful to distinguish the address
3. Process the URl export of the new API-MORk address
function modifyUrl (port, halfUrl) {
return 'http://j-api.jd.com/' + 'mocker/data?p=' + port + '&v=' + method.toLocaleUpperCase() + '&u=/' + halfUrl
}
Copy the code